2013-08-30 40 views
0

嗨,伙计们!骨干 - 视图没有方法'得到'

我刚开始学习Backbone,我的第一个代码有问题。我做的示例代码中有一个按钮,一旦点击该按钮,就会添加一段代码(View),该代码应该从模型中获取一些默认值。但我收到一条消息:Object function(){return i.apply(this,arguments)} has no method 'get'。这里是我的代码:

var app = {}; 

    // the model 
    app.Answer = Backbone.Model.extend({ 
    defaults: { 
     title: 'Your answer here', 
     isAnswer: false 
    } 
    }); 

    //the collection 
    app.AnswerList = Backbone.Collection.extend({ 
    model: app.Answer, 
    localStorage: new Store("backbone-answers") 
    }); 

    //the view that is added when the button is clicked  
    app.AnswerView = Backbone.View.extend({ 
    tagName: 'li', 
    // template: _.template($('#answer-template').html()), 
    template: _.template('<%= title %>: <%= isAnswer %>'), 

    events: { 
     'click button.destroy': 'remove', 
    }, 

    initialize: function(){ 
     _.bindAll(this, 'render','remove'); 
     // this.model.bind('change', this.render()); 
     this.render(); 
    }, 
    render: function(){ 
     $(this.el).html(this.template({title: this.model.get('title'), isAnswer: this.model.get('isAnswer')})); 
     return this; 
    }, 
    remove: function(){ 
     $(this.el).destroy(); 
    } 
    }); 

    // the main view 
    app.AppView = Backbone.View.extend({ 
    el: $('#body'), 

    events: { 
     'click button#insert-button': 'addAnswer' 
    }, 

    initialize: function(){ 
     _.bindAll(this, 'addAnswer'); 
     this.ul = $('#content'); 
     this.collection = new app.AnswerList(); 
     console.log('initialize'); 
    }, 
    addAnswer: function(){ 
     console.log('append'); 
     var newAnswer = new app.AnswerView({model: app.Answer}); 
     $('ul',this.el).append(newAnswer); 
    } 

    }); 

    app.appView = new app.AppView(); 

我是Backbone的新手,所以我的问题是:我做错了什么?

感谢您的帮助!

+1

传递给你的app.AnswerView实例模型应该是你app.Answer模型的实例,而不是在构造函数的引用。 – kinakuta

+0

嗨@kinakuta,谢谢你的提示!你说,这部分:'var newAnswer = new app.AnswerView({model:app.Answer});',对吧?如果我只是把'var newAnswer = new app.AnswerView();',我得到这个错误:'不能调用未定义的方法'获取'。但是,如果我把'var newAnswer = new app.AnswerView({model:new app.Answer});',我没有错误,但视图不被追加。你知道为什么? –

回答

1

尝试

var answer = new app.Answer(); 
var newAnswer = new app.AnswerView({model: answer}); 
+0

谢谢@Vadim!用你的例子,我也看到@kinakuta的意思了!我只是在将模型添加到视图之前未创建模型的实例。我也看到了@Sebastian代码,但是分析这两种方法,我发现你的例子为对象返回了一个正确的'cid'(client id)序列('cid:view3','cid:view4','cid:view5' ...),而@Sebastian方法返回一个“跳转”数字的序列('cid:view3','cid:view5','cid:view7' ...)。我不知道这会带来什么后果,但我更喜欢这种方法使用正确的数字来表示'cid'。 –

1

尝试使用此代码。 创建新视图时,您必须实例化模型。

var app = {}; 

    // the model 
    app.Answer = Backbone.Model.extend({ 
    defaults: { 
     title: 'Your answer here', 
     isAnswer: false 
    } 
    }); 

    //the collection 
    app.AnswerList = Backbone.Collection.extend({ 
    model: app.Answer, 
    localStorage: new Store("backbone-answers") 
    }); 

    //the view that is added when the button is clicked  
    app.AnswerView = Backbone.View.extend({ 
    tagName: 'li', 
    // template: _.template($('#answer-template').html()), 
    template: _.template('<%= title %>: <%= isAnswer %>'), 

    events: { 
     'click button.destroy': 'remove', 
    }, 

    initialize: function(){ 
     _.bindAll(this, 'render','remove'); 
     // this.model.bind('change', this.render()); 
     this.render(); 
    }, 
    render: function(){ 
     $(this.el).html(this.template({title: this.model.get('title'), isAnswer: this.model.get('isAnswer')})); 
     return this; 
    }, 
    remove: function(){ 
     $(this.el).destroy(); 
    } 
    }); 

    // the main view 
    app.AppView = Backbone.View.extend({ 
    el: $('#body'), 

    events: { 
     'click button#insert-button': 'addAnswer' 
    }, 

    initialize: function(){ 
     _.bindAll(this, 'addAnswer'); 
     this.ul = $('#content'); 
     this.collection = new app.AnswerList(); 
     console.log('initialize'); 
    }, 
    addAnswer: function(){ 
     console.log('append'); 
     var newAnswer = new app.AnswerView({model: new app.Answer()}); 
     $('ul',this.el).append(newAnswer); 
    } 

    }); 

    app.appView = new app.AppView(); 
+0

谢谢你的回答,@塞巴斯蒂安!尽管这很有效,但我发现在视图之外实例化模型,然后调用它(如@Vadim方法)会返回正确数量的'cid',我更喜欢它。 –