0
  • 我再次编辑我的代码,我收到以下 2 个错误:

1) Complex_collection 未定义

  model: Complex_model

2) Complex_model 未定义

 model: Complex_model

我正在使用主干 js 以下是我的代码,我通过使用主干.js 完成了此操作,但我的代码没有运行我想要使用主干 js 单独的模型、视图和控制器

//模型

(function(){ var Complex_model = Backbone.Model.extend({        
    sync:function() {},
    validate:function() {},
    url:function() {},          
    defaults :{
        name : null
    }    
});})(this);

//看法

(function(){

FriendView = Backbone.View.extend({
    events :{
        'click #add-input' : 'add'
    },
    initialize: function(){
        this.collection = new Complex_collection(); // This is collection
        _.bindAll(this, 'render');
    },      
    add : function() {
        alert("hello");
        var friend_name = $('#input').val();
        this.collection.add({ name : friend_name });
    },
    render : function(){
        $("#friends-list").append("<li>"+ model.get("name")+"</li>");
    },      
});

var view = new FriendView(); })(this);

//收藏

(function(){    

    var Complex_collection = Backbone.Collection.extend({

    model: Complex_model

});})(this);

谢谢你

4

1 回答 1

0

您不应在初始化代码中创建同一对象的新实例,您的代码应如下所示:

(function(){ 
    this.complex_model = Backbone.Model.extend({        
        sync:function() {},
        validate:function() {},
        url:function() {},      
    });
})(this);

// View

(function(){
    var vw = view = function(){};
    FriendView = Backbone.View.extend({
        events :{
            'click #add-input' : 'add'
        },
        initialize: function(){
            this.friendslist = new FriendList(); // This is your collection right?
            _.bindAll(this, 'render');
        },      
        add : function() {
            var friend_name = $('#input').val();
            this.friendslist.add({ name : friend_name });
        },
        render : function(){
            $("#friends-list").append("<li>"+ model.get("name")+"</li>");
        },      
    });
})(this);

// Collection
(function(){    this.complex_collection = Backbone.Collection.extend({
    model : complex_model
});})(this);

// Your main app
var view = new FriendView();
于 2012-05-09T06:23:19.263 回答