Backbone.js-模型中的集合-对象函数(a){return new n(a)}没有方法'有'
Backbone.js - Collection within Model - Object function (a){return new n(a)} has no method 'has'
我收到
Object function (a){return new n(a)} has no method 'has'
在我的模型上调用fetch()
方法时出错。这是代码:
var Exercise = Backbone.Model.extend({
defaults: {
idAttribute: 'e_id',
e_id: "-1",
exerciseName: "Exercise",
exerciseDescription: "Address",
exerciseURL: "vimeo.com",
reps: "0",
sequence: "0"
},
initialize: function() {
alert("Exercise!");
}
});
var ExerciseList = Backbone.Collection.extend({
url: "/getWorkoutList.php",
model: Exercise,
initialize: function() { }
});
var Workout = Backbone.Model.extend({
urlRoot: "/getWorkoutList.php",
url: function() {
return this.urlRoot + "?workoutID=" + this.get('workoutId');
},
defaults: {
idAttribute: 'workoutId',
workoutId: "-1",
workoutName: "WorkoutName",
workoutDescription: "WorkoutDescription",
exercises: new ExerciseList()
},
initialize: function() {
_.bindAll(this);
directory.renderWorkout(this);
},
parse: function(response) {
return response;
}
});
var WorkoutList = Backbone.Collection.extend({
url: "/getWorkoutList.php",
model: Workout,
initialize: function() {
_.bindAll(this);
},
parse: function(response) {
return response;
}
});
var WorkoutView = Backbone.View.extend({
tagName: "div",
className: "workout-container",
template: $("#tmp-workout").html(),
initialize: function() {
_.bindAll(this);
this.model.bind('change', this.render, this);
},
render: function(){
console.log("WorkoutView");
var tmpl = _.template(this.template);
this.$el.html(tmpl(this.model.toJSON()));
return this;
},
//add ui events
events: {
"click #workout-details": "getWorkoutDetails"
},
getWorkoutDetails: function (e) {
e.preventDefault();
this.model.fetch();
}
});
var ExerciseView = Backbone.View.extend({
tagName: "exercise",
className: "exercise-container",
template: $("#tmp-exercise").html(),
initialize: function() {
_.bindAll(this);
alert("ExerciseView");
},
render: function(){
console.log("render exercise view");
var tmpl = _.template(this.template);
this.$el.html(tmpl(this.model.toJSON()));
return this;
}
});
var WorkoutListingView = Backbone.View.extend({
el: $("#workouts"),
initialize: function() {
var collection = new WorkoutList();
collection.fetch();
},
render: function() {
var that = this;
_.each(this.collection.models, function(item){
that.renderWorkout(item);
});
},
renderWorkout: function(item) {
var workoutView = new WorkoutView({
model:item
});
this.$el.append(workoutView.render().el);
var that = this;
_.each(workoutView.model.get('exercises').models, function(exercise) {
that.renderExercise(exercise);
});
},
renderExercise: function(item) {
var exerciseView = new ExerciseView({
model:item
});
this.$el.append(exerciseView.render().el);
}
});
当我第一次检索锻炼系列时,一切都很好。但是,当我调用getWorkoutDetails
时,我会得到错误。通过在锻炼模型的parse()
中插入警报和console.logs
,我发现它确实从服务器得到了正确的响应,但由于某种原因,它给出了这个错误。
有什么想法吗?谢谢
好吧,在花了很多时间在缩小javascript意大利面条的美丽世界中之后,我发现我使用的undercore.js版本中没有"has"函数。将underscore.js从1.2.2更新到1.4.4解决了这个问题。此外,我的backbone.js版本是0.9.1
相关文章:
- 为什么dijit.form.Select有方法setStore(),而dijit.fform.FilteringSele
- Backbone.js视图可以有一个模型和一个集合吗
- 余烬.js有许多模型关系保存
- JSPM-是否有方法或需要对捆绑文件进行版本化
- 当用户试图删除indexedDB数据库时,是否有方法执行函数
- Box2dWeb中是否有方法将对象的位置.x输出到console.log()
- 是否有方法在WebCrypto API中获取RSA密钥的组件
- 是否有方法可以快速确定浏览器是否支持启用了cors的图像而不会污染浏览器?
- 是否有方法以编程方式确定私钥是用DER还是PEM编码的?
- 在反序列化JSON对象时,是否有方法将属性名称转换为小写?
- 是否有方法检查特定点(X,Y)是否在SVG元素中
- 是否有方法减少相同if语句的代码
- 如何向控制器发送数据,比如Model有列表模型AngularJs
- 在babel编译时是否有方法注入或忽略全局变量?
- Node.js query:是否有方法提示用户自定义大小的数组
- 在检查字符串时,是否有方法包含单词变体(过去时,动名词)
- 当元素不处于悬停状态时,是否有方法获得元素的HOVER样式?
- 是否有方法检测和更改输入值格式
- 解析JSON字符串后是否有方法获得对象属性
- 检查对象是否有方法,而不实例化它