2017-05-02 24 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());