0

在我的应用程序中,我有多个页面,我确实有一个普通模型,从普通模型中,我使用集合的parse方法根据页面扩展更多模型。

这样做之后,我仍然没有得到返回的集合,而是得到了我从后端检索的对象。

我的视图代码:

 define([
        "jquery",
        "backbone",
        "models/model",
        "collection/dashBoardcollection",
        "views/navi/naviView",
        "views/dashBoard/myTaskView",
        ],function ($,Backbone,model,collection,naviView,myTaskView) {

    var dashBoardView = Backbone.View.extend({
        el:$("div.contentwrapper"),
        events:{
            "click h2" : "tileleDispaly"
        },
        initialize:function(){
            this.$el.html(this.template);
            this.c = collection;
            this.listenTo(this.c, 'sync', this.logCollection);
            this.c.fetch();
        },
        logCollection:function(){
             console.log(this.c);
//not getting the collection shows array length as 1
            },
            tileleDispaly:function(e){
                var tittle = $(e.target).text();
                new myTaskView().showTitle(tittle);
            }
        });

    return dashBoardView;

})

我的收藏代码:

   define(["backbone","models/model"], function (Backbone,model) {

    var titleModel = model.extend({
        "title" : "no title assigned"
    })

    var slideModel = model.extend({
        "slide" : "no title assigned"
    })

    var rowModel = model.extend({
        "title" : "no title assigned",
        "text"  : "no text",
        "date"  : "define date"
    })

    var optionModel = model.extend({
        "show" : "no title assigned"
    })

    var dashBoardCollection = Backbone.Collection.extend({
        url:"js/dashBoard.json",
        model:model,
        initialize:function(model,options){

        },
        parse:function(response){

            var that = this;

                var x = [];
// update 1. pushing all models to x array

            _.each(response.DashBoard, function(obj){

                if(obj.hasOwnProperty("title")){

                    x.push(new titleModel(obj));

                }else if (obj.hasOwnProperty("widget")){

                    _.each(obj.widget, function(m){
                        x.push(new slideModel(obj));
                    })

                }else if (obj.hasOwnProperty("activity")){

                    _.each(obj.activity, function(va){

                        if(va.hasOwnProperty("rows")){

                            _.each(va.rows, function(row){
                                x.push(new rowModel(row));
                            })

                        }else if (va.hasOwnProperty("option")){

                            _.each(va.option, function(option){
                                x.push(new optionModel(option));
                            })

                        }

                    })

                }

            })

            this.add(x);
//update 2. adding the models to collection

                console.log(this);
// console works fine shows the array length as 12.

                return this;
//returning to view.. 

            }
        });

    return new dashBoardCollection;

})

但我的观点没有带来任何模型..从收藏中,这里有什么问题..?还是我的方法是错误的?任何人都可以帮我解决这个问题..?

4

1 回答 1

1

那是因为 done 中的响应不是 Collection。它只是响应中的一个对象..

日志 c 是一个更新的集合

c.done(function(data){
    console.log(c); //Will be the updated collection
})

与使用承诺的方法相比,监听事件也是一种更清洁的方法。

initialize: function() {
       this.$el.html(this.template);
       this.c = collection;
       this.listenTo(this.c, 'sync', this.logCollection);

       this.c.fetch(); //fetching the data
},
logCollection: function() {
    console.log(this.c); // This will log the collection
}
于 2013-08-01T16:43:28.123 回答