2017-05-02 26 views
0

我開始knockoutjs,但我想知道我怎麼能做到這一點。我想添加src屬性哪一個將包含靜態和動態內容。請查看底部knockoutjs viewmodel綁定與阿賈克斯和計算

var viewModel = { 
    title : ko.observable("title"), 
    description : ko.observable("description"), 
    poster : ko.observable("poster"), 
    imdb : ko.observable("imdb") 
    movie : ko.computed(function(){ 
     return 'https://www.youtube.com/embed?listType=search&list='+this.title()+'&autoplay=1&origin=http://filmmovies.in&fs=0&modestbranding=1&rel=0&loop=1&color=white&controls=0&mode=opaque' 
    }) 
}; 
$.post('/movies/index').then(function(data){ 
    ko.applyBindings({movies: data}); 
}); 

======================================== 請求響應

[ 
    {"title":"Iron Man","description":"asdf kasdf <H1>hsdifaf</H1>","poster":"http://www.google.com","imdb":"[email protected]","createdAt":"2017-05-02T07:42:28.864Z","updatedAt":"2017-05-02T07:42:28.864Z","id":10}, 
    {"title":"Iron Man","description":"asdf kasdf <H1>hsdifaf</H1>","poster":"http://www.google.com","imdb":"[email protected]","createdAt":"2017-05-02T06:53:51.984Z","updatedAt":"2017-05-02T06:53:51.984Z","id":1}, 
    {"title":"Iron Man","description":"asdf kasdf <H1>hsdifaf</H1>","poster":"http://www.google.com","imdb":"[email protected]","createdAt":"2017-05-02T06:55:20.245Z","updatedAt":"2017-05-02T06:55:20.245Z","id":3}, 
    {"title":"Iron Man","description":"asdf kasdf <H1>hsdifaf</H1>","poster":"http://www.google.com","imdb":"[email protected]","createdAt":"2017-05-02T07:12:45.620Z","updatedAt":"2017-05-02T07:12:45.620Z","id":4} 
] 
+0

是來自請求的數據是數組嗎? –

+0

是來自請求的數據是一個數組,但是數組的每個元素都是json對象 –

+0

您使用共享的代碼面臨什麼問題?你想通過這個 - http://stackoverflow.com/help/how-to-ask – gkb

回答

0

通過這個撥弄它會給你如何使用上市

http://jsfiddle.net/emrefatih47/Mmt9F/ 這是一個樣本,這是你在找什麼實際的 //包含的模型

function SomeModel() { 
    this.Firstname = ko.observable(); 
    this.Lastname = ko.observable(); 
} 

// View Model 


function SomeViewModel() { 
    var self = this; 

    this.ArrayOfModels = ko.mapping.fromJS([]); 

    this.GetModelsByAjax = function() { 
     $.ajax({ 
      type: 'POST', 
      url: '/echo/json/', 
      data: { 
       json: JSON.stringify([{ 
       Firstname: "Bob", 
       Lastname: "Smith"}, 
      { 
       Firstname: "Ted", 
       Lastname: "Smith"}]), 
       delay: 0 
      }, 
      context: this, 
      success: function(data) { 
       self.SuccessfullyRetrievedModelsFromAjax(data); 
      }, 
      dataType: 'json' 
     }); 
    }; 

    this.SuccessfullyRetrievedModelsFromAjax = function(models) { 
     ko.mapping.fromJS(models, self.ArrayOfModels); 
    } 

; }

ko.applyBindings(new SomeViewModel());