Backbone.js添加事件问题

时间:2011-08-24 23:36:48

标签: backbone.js

我有以下backbone.js代码,我在该事件中遇到问题,然后我从集合中触发“添加”事件。在this.field.add(list_fields);提取中添加success:会导致错误。如何确保获取模型,然后在

之后运行add事件
$(function() {
        $(".chzn-select").chosen();
        /*********************************Models*************************************************/
        var Table = Backbone.Model.extend({

            urlRoot : '/campusfeed/index.php/welcome/generate'
        });
        var Field = Backbone.Model.extend({
            urlRoot: '/campusfeed/index.php/welcome/generate' 
        });
        /**************************Collections*************************************************/    
        Tables = Backbone.Collection.extend({
            //This is our Friends collection and holds our Friend models
            initialize : function(models, options) {
                this.bind("add", options.view.addFriendLi);
            //Listen for new additions to the collection and call a view function if so
            }
        });

        var Fields = Backbone.Collection.extend({
            model:Field,
            url:'http://localhost/campusfeed/index.php/welcome/generateFields',
            initialize : function(models, options) {
                this.bind("add", options.view.getFields);
            }
        });
        /************************************Views**************************************************/
        var m="shit";
        var app = Backbone.View.extend({
            el:'body',
            initialize:function(model,options){
                //Create collections in here

                this.table = new Tables(null,{
                    view : this
                });
                this.field = new Fields(null,{
                    view : this
                });
            },
            events : {
                "click #choose" : "generate"

            },
            generate:function(){
                var table = ( this.$("#table option:selected").text());
                var dbname = ( this.$("#database").text());
                var list_fields = new Field();
                list_fields.urlRoot = list_fields.urlRoot+"/"+dbname+"/"+table;
                list_fields.fetch({
                    success:function(){
                        console.log(JSON.stringify(list_fields));


                    }
                });

                this.field.add(list_fields);

            },

            getFields:function(model){

               console.log(JSON.stringify(model));


            }



        });
        var apprun = new app;
    /* var data = new Fields();
        data.url=data.url+"/some/data";
        alert(data.url);
        data.fetch();
        var staff = new Table();
        staff.fetch();
        var field = new Field();*/
    });

1 个答案:

答案 0 :(得分:1)

问题是“这个”的背景。成功回调函数将“this”设置为list_fields。你可以用“自我”或“那个”变量解决这个问题:

        generate:function(){
            var table = ( this.$("#table option:selected").text());
            var dbname = ( this.$("#database").text());
            var list_fields = new Field();
            list_fields.urlRoot = list_fields.urlRoot+"/"+dbname+"/"+table;
            var that = this;
            list_fields.fetch({
                success:function(){
                    console.log(JSON.stringify(list_fields));

                    that.field.add(list_fields);
                }
            });
        },

作为旁注 - 您的馆藏永远不应该有对视图的引用。相反,您的视图应引用该集合并绑定到集合事件

    var Fields = Backbone.Collection.extend({
        model:Field,
        url:'http://localhost/campusfeed/index.php/welcome/generateFields',
    });

    var app = Backbone.View.extend({
        initialize:function(model,options){
            //Create collections in here

            this.field = new Fields();
            this.field.bind("add", this.getFields, this);
        },
        getFields: function(){ ... }
    });