Backbone.js的的toJSON()不包括属性 [英] Backbone.js toJSON() not including attributes

查看:104
本文介绍了Backbone.js的的toJSON()不包括属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不能满脑子都在这一个。出于某种原因,(我怀疑它的的东西的异步请求做),使用得当的toJSON()函数,我的模型属性不被转换。 FWIW,我试图做延迟加载在Django关系fetchRelated()。

下面就是我所做的fetchRelated()请求,并创建一个新视图。

  $。当(usermodel.fetchRelated(电影))。然后(函数(){
        VAR NewView的=新UserMovieList({模式:的usermodel});
        newview.render();
    });

这会调出以下渲染功能:

 渲染:功能(){
        $(this.el)的.html(this.template({}));        VAR的usermodel = this.model;        VAR包装= $(电影。);
            VAR配方= usermodel.get(电影);
            movies.each(功能(电影){
                VAR movie_item =新UserMovieListItem({
                    型号:电影
                });
                movie_item.render();
                wrapper.append(movie_item.el);
            });
        返回此;

然后调用UserMovieListItem渲染功能:

 渲染:功能(){
        的console.log(电影JSONify,this.model.attributes);
的console.log(电影JSONified,this.model.toJSON());
        $(this.el)的.html(this.template(this.model.toJSON()));
        返回此;

这就是它与众不同。第一次的console.log(this.model.attributes)显示所有的属性,因为他们应该的。这似乎完全正常。但(this.model.toJSON())只返回了其中的fetchRelated()之前提供的URI和id属性。到底是什么怎么回事?如果我绑定this.model在UserMovieListItem与变革来渲染,那么它的将会的渲染,但经过半秒左右...
        }

我的用户模型定义为这样的:

  window.User = Backbone.RelationalModel.extend({
    urlRoot:'/电影/ API / V1 /用户/',
    关系:[{
        类型:Backbone.HasMany,
        键:电影,
        relatedModel:'电影',
        collectionType:UserMovieCollection',
        reverseRelation:{
            键:用户,
            includeInJSON:ID
        }
    }],
    选择:功能(){
        VAR模型=这一点;
        model.set({选择:真});
    }
});


解决方案

fetchRelated 返回 jqXHR 对象的数组,你需要用$。当这样使用它:

  $。when.apply(NULL,usermodel.fetchRelated(电影))。然后(函数(){
    VAR NewView的=新UserMovieList({模式:的usermodel});
    newview.render();
});

I really can't wrap my head around this one. For some reason, (and I suspect it has something to do with asynchronous requests) my model attribute is not being converted properly using the toJSON() function. FWIW, I'm attempting to do lazy loading with django-relational fetchRelated().

Here's where I made the fetchRelated() request and create a new view.

$.when(usermodel.fetchRelated("movies")).then(function(){
        var newview = new UserMovieList({model: usermodel});
        newview.render(); 
    });

And this calls up the following render function:

render: function(){
        $(this.el).html(this.template({}));

        var usermodel = this.model; 

        var wrapper = $(".movies");
            var recipes = usermodel.get("movies");
            movies.each(function(movie){
                var movie_item = new UserMovieListItem({
                    model:movie
                });
                movie_item.render(); 
                wrapper.append(movie_item.el);
            });
        return this; 

Which then calls the UserMovieListItem render function:

render: function(){
        console.log("movies to JSONify", this.model.attributes);
console.log("movies JSONified", this.model.toJSON());
        $(this.el).html(this.template(this.model.toJSON()));
        return this; 

Here's the kicker. The first console.log (this.model.attributes) displays all of the attributes as they should be. It seems perfectly fine. But (this.model.toJSON()) only returns the URI and id attributes which were provided before the fetchRelated(). What the heck is going on here? If I bind this.model in UserMovieListItem with 'change' to render, then it will render, but after half a second or so... },

My User model is defined as such:

window.User = Backbone.RelationalModel.extend({
    urlRoot: '/movie/api/v1/users/',
    relations: [{
        type: Backbone.HasMany,
        key: 'movies',
        relatedModel: 'Movie',
        collectionType: 'UserMovieCollection',
        reverseRelation: {
            key: 'user',
            includeInJSON: 'id'
        }
    }],
    select: function(){
        var model = this; 
        model.set({selected: true});
    }
});

解决方案

fetchRelated returns array of jqXHR objects, and you need to use it with $.when this way:

$.when.apply(null, usermodel.fetchRelated("movies")).then(function(){
    var newview = new UserMovieList({model: usermodel});
    newview.render(); 
});

这篇关于Backbone.js的的toJSON()不包括属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆