gpt4 book ai didi

javascript - 主干 - 查看没有方法 'get'

转载 作者:行者123 更新时间:2023-12-02 18:24:20 25 4
gpt4 key购买 nike

大家好!

我刚刚开始学习 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 的新手,所以我的问题是:我做错了什么?

感谢您的帮助!

最佳答案

尝试

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

关于javascript - 主干 - 查看没有方法 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18536307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com