从视图中的模型获取属性的主干
Backbone getting attributes from a model in a view
我需要帮助来尝试从我的模型中获取具有骨干的属性.js
以下是我到目前为止尝试过的方法。我连接到 REST URL 并以 json 格式拉回数据。我现在希望在视图中显示其中一些数据。但是,当我尝试打印/控制台输出club_url
时,我收到一个未定义的错误。如果我打印出test
对象本身,我可以看到对象attributes section
中的值。
有人可以告诉我哪里出错了吗?
(function ($) {
var Model = Backbone.Model.extend({
urlRoot: '/api/test/',
initialize: function () {
this.club_url = this.club_url
}
});
var thisCollection = Backbone.Collection.extend({
urlRoot: '/api/test/',
model: Model
});
var PanelView = Backbone.View.extend({
el: '#reward_view',
initialize: function () {
_.bindAll(this, 'render');
this.collection = new thisCollection();
this.collection.bind('add', this.appendItem);
this.render();
},
render: function () {
var test = new thisCollection;
test.fetch();
console.log(test.get('club_url'))
return this;
}
});
var listView = new PanelView();
})(jQuery);
我尝试的另一个测试是在视图中初始化这样的东西
this.model = new Model()
this.model.fetch()
但是在渲染函数中我这样做了:
this.model.get('club_url')
然而,这也不起作用!
数据的获取是异步操作。所以,我想你应该等一个事件再来club_url
.即类似的东西:
render: function () {
var test = new thisCollection;
test.fetch({
success: function(collection, response) {
console.log(test.get('club_url'))
}
});
return this;
}
相关文章:
- 通过id和class属性获取元素
- 使用javascript从节点属性获取数组值
- 如何在jquery中通过自定义属性获取输入字段的值
- 通过特定的数据属性获取元素
- 如何使用js/jquery将数组中的数据属性获取到DOM上的h3中
- 在 GWT 中按属性获取 DOM 元素
- 将从元素的数据属性获取的字符串转换为 json
- 角度表达式从对象数组中的相等属性获取对象的属性
- 仅从背景图像属性获取 URL
- 通过 Javascript 从 href 属性获取查询字符串
- 如何使用事件触发器和自定义属性获取元素的子元素
- jQuery从标题属性获取动态生成的内容
- 单击时从数据属性获取更新的值
- jQuery 从数据属性获取路径
- 使用属性获取对象值的运行时间
- 通过 src 属性获取元素,使用 javascript 而不是 jquery
- 如何从 href 属性获取默认 id 值
- 使用 javascript 按属性获取 iframe 的元素
- 使用 jQuery 按名称属性获取选择值
- 硒:借助链接等属性获取元素的路径