knockoutjs viewmodel与ajax绑定并计算

时间:2017-05-02 08:30:50

标签: jquery ajax knockout.js

我从敲击开始,但我想知道我怎么能这样做。我想添加一个src属性,其中包含静态和动态内容。请检查底部

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":"viapn.webdeveloper@gmail.com","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":"viapn.webdeveloper@gmail.com","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":"viapn.webdeveloper@gmail.com","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":"viapn.webdeveloper@gmail.com","createdAt":"2017-05-02T07:12:45.620Z","updatedAt":"2017-05-02T07:12:45.620Z","id":4}
]

1 个答案:

答案 0 :(得分: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());