English 中文(简体)
JSON () 不包括属性的后框bone. js to JSON ()
原标题:Backbone.js toJSON() not including attributes

我实在无法环绕这一个任务。 出于某种原因, (我怀疑它与非同步请求有关) 我的模型属性没有使用 TOJSON () 函数进行正确的转换。 FWWW, 我试图用 django- relational relational relatet related () 进行懒惰的装入 。

s 在这里我提出了freetlecont() 请求并创建了新视图 。

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

这就要求发挥以下作用:

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; 

然后称为用户MovieListItem 产生功能 :

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... },

我的用户模式定义如下:

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});
    }
});
最佳回答

fetchelate 返回 jqXHR 对象的阵列, 您需要用$来使用 。 当此方式 :

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

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签