Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js添加事件问题_Backbone.js - Fatal编程技术网

Backbone.js添加事件问题

Backbone.js添加事件问题,backbone.js,Backbone.js,我有下面的backbone.js代码,在从集合中获取“add”事件之前,我在该事件中遇到了一个问题。添加this.field.add(列表字段)导致错误。如何确保获取模型,然后运行add事件 $(function() { $(".chzn-select").chosen(); /*********************************Models*************************************************/

我有下面的backbone.js代码,在从集合中获取“add”事件之前,我在该事件中遇到了一个问题。添加
this.field.add(列表字段)success:
fetch中的code>导致错误。如何确保获取模型,然后运行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();*/
    });

问题在于“这个”的背景。成功回调函数将“this”设置为列表字段。您可以使用“self”或“that”变量来解决此问题:

        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(){ ... }
    });