“添加:模型”未触发骨干关系

时间:2012-05-22 08:53:55

标签: google-app-engine backbone.js backbone-relational

我刚刚开始研究Backbone-relational,我遇到了“HasMany”案件的问题。

首先是模型:

    var Word = Backbone.RelationalModel.extend({
    urlRoot: '/test/api/word',
    idAttribute: '_id'
});

var List = Backbone.RelationalModel.extend({
    urlRoot: '/test/api/list',
    idAttribute: '_id',
    relations: [{
        type: Backbone.HasMany,
        key: 'words',
        relatedModel: 'Word',
        reverseRelation: {
            key: 'list',
            includeInJSON: '_id'
        }
    }]
});

“List”模型的视图,其中“单词”应该按如下方式呈现:

    var ListWordsView = Backbone.View.extend({
    tagName: 'div',


    initialize: function(){
        _.bindAll(this,'render', 'renderWords');
        //alert("its clicked!," + this.model.get('desc'));
        this.model.bind('change',this.render);
        this.model.bind('reset',this.render);
        this.model.bind('add:words',this.renderWords);
    },

    renderWords: function(word){
        alert("hello it's there!" + word.get("word_name"));
        var wordView = WordView({ model: word });
        this.$("#words_list").append(wordView.render());
    },
    render: function(){
        try{
        $("#list_box.title_name").html(this.model.get('list_name'));
        this.model.toJSON();
        //this.model.get('words').each(renderWords);
        //this.setContent();
        alert(JSON.stringify(this.model));
        }
        catch(e){
            alert("error! : " + e.message);
        }
    } 
});

所以问题在于调用这个ListWordsView时(在完成List的fetch()之后),“add:words”没有被触发,因此没有显示单词。

尝试了一些方法,但没有奏效。我呈现List本身或其他什么方式有什么问题吗?

0 个答案:

没有答案