访问模板中的属性时出现问题-除'name'显示错误'没有定义[attributename];
Trouble accessing attributes in the template - all attributes apart from 'name' show error '[attributename] is not defined'
问题是,当从模板访问时,除了"name"之外的所有Object属性都会在控制台中调用错误"id/url/athing is not defined"。只有"name"的模板显示良好并显示正确的名称,但一旦我调用不同的属性,例如id
或url
,它就会中断。传递给视图的对象是一个经过解析的静态JSON文件,所有项目都位于同一级别,可以通过控制台访问,例如collectionName.models[0].get('id');
让我感到困惑的是,name属性是有效的,就好像它是在主干/下划线代码中的某个地方预定义的一样。
我是不是错过了一些显而易见的东西?由于我可以从控制台访问模型数据,我认为视图本身处理数据的方式有问题,但我尝试过用几种不同的方式重写它,但似乎没有什么不同。
所有相关代码。
传递的对象格式。这也是collectionName.models[0].attributes;
在控制台中返回的内容。
[{
"id":"0",
"name": "Building1",
"url": "building_1",
"floors":[{
"id":"0",
"name":"Ground Floor",
"image":"",
"rooms":[{
"id": "r_1",
"name": "Room 1",
},
{
"id": "r_2",
"name": "Room 2"
}]
}
}]
}
示例模板代码:
<span class="name"><%= name %></span>
<%= id %> <%= url %>
路由器代码:
routes: {
'': 'intro', // this route is using pretty much identical code and works fine, the model has the exact same format, the only difference is that all attributes work.
':id': 'firstLevel'
},
firstLevel: function (id) {
window.singleBuilding = new ThisBuilding({}, {idBuilding: id});
window.singleBuilding.fetch();
this.floorView = new FloorList({
collection: window.singleBuilding
});
var $intro = $('#intro');
$intro.empty();
$intro.append(this.floorView.render().el);
}
视图:
window.FloorSingleList = Backbone.View.extend({
className: 'floor-list',
initialize: function () {
this.template = _.template(tpl.get('floors-list-item'));
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.testModel = this.model.attributes; // I tried passing the attributes directly to the templatewithout .toJSON(), which worked exactly the same, as in only the 'name' attribute worked
},
render: function () {
console.log("The test data is:", this.testModel);
console.log("The actual model data is:", this.model);
var renderedContent = this.template(this.model.toJSON());
$(this.el).html(renderedContent);
return this;
}
});
window.FloorList = Backbone.View.extend({
tagName: 'section',
className: 'intro-list',
initialize: function () {
this.template = _.template(tpl.get('intro-list'));
_.bindAll(this, 'render');
this.collection.bind('reset', this.render, this);
this.collection.bind('change', this.render, this);
},
render: function (eventName) {
var $introList;
var collection = this.collection;
$(this.el).html(this.template({ }));
$introList = this.$('.intro-list');
collection.each(function (building) {
var view = new FloorSingleList({
model: building,
collection: collection
});
$introList.append(view.render().el);
});
return this;
}
});
型号代码:
window.ThisBuilding = Backbone.Collection.extend({
model : Building,
initialize: function(models, options) {
// Initialising the argument passed on from the router.
this.idBuilding = options.idBuilding;
return this;
},
url : function(){
return "data.json"
},
parse: function (response) {
console.log("Passed parameters are :", this.idBuilding); // Returns the request parameters passed from the router.
return response[this.idBuilding];
}
});
模板&引导
// templates are loaded during the bootstrap
tpl.loadTemplates(['header', 'intro-list', 'floors-list-item', 'building-list-item'], function() {
window.App = new ExampleApp();
Backbone.history.start();
});
问题在于javascript中的fetch是如何异步的。。。
firstLevel: function (id) {
window.singleBuilding = new ThisBuilding({}, {idBuilding: id});
window.singleBuilding.fetch(); // YOU FETCH HERE
this.floorView = new FloorList({
collection: window.singleBuilding
});
var $intro = $('#intro');
$intro.empty();
$intro.append(this.floorView.render().el); // AND RENDER WHILE YOU CAN'T ASCERTAIN THE FETCH HAS BEEN COMPLETED...
}
因此,渲染会尝试读取尚未正确初始化的集合->您的模型尚未完成->有趣的读数。控制台日志在异步操作方面发挥着神奇的作用。所以它可能告诉你一些事情,而现实是完全不同的。所以改为这样做:
firstLevel: function (id) {
window.singleBuilding = new ThisBuilding({}, {idBuilding: id});
// Don't fetch here...
this.floorView = new FloorList({
collection: window.singleBuilding
});
var $intro = $('#intro');
$intro.empty();
$intro.append(this.floorView.el); // Don't render here
}
然后在FloorList
-视图中:
initialize: function () {
this.template = _.template(tpl.get('intro-list'));
_.bindAll(this, 'render');
this.collection.bind('reset', this.render, this);
this.collection.bind('change', this.render, this);
this.collections.fetch(); // fetch here, your binds take care of rendering when finished
}
更新2:显然,我看到了没有复杂性的地方。。。忽略下面的所有内容
来自Backbone.js文档,在Model.toJSON()
上
返回用于JSON字符串化的模型属性的副本。
因此,它以JSON的形式返回属性。现在主干网将id
定义为
模型的一个特殊属性。。。
属性,而不是属性。url也是如此。在表示骨干模型的javascript对象中,属性存储在对象中自己的对象中,id和url属性存储在模型对象的其他位置。例如,表示您的模型的javascript对象可能看起来像这样:
{
...
attributes: Object // this is where your name attribute lives
...
id: 34, // this is your id property
...
__proto__: ctor, // this is where your url-function resides
...
}
UPDATE:id属性嵌入到model.toJSON()
中的JSON中
因此,当您执行this.model.toJSON()
时,您可以根据模型对象属性属性的内容创建一个JSON,并包含id属性。url-属性不包含在其中。你可以做什么,例如:
var renderedContent = this.template({
attributes: this.model.toJSON(),
url: this.model.url()
});
并且在模板中
<span class="name"><%= attributes.name %></span>
<%= attributes.id %> <%= url %>
希望这能有所帮助!
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何在生成下载文件时显示加载动画
- React重新渲染但未显示正确的组件
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 音频控件在mouseover上显示,在mouseout上淡出
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 访问模板中的属性时出现问题-除'name'显示错误'没有定义[attributename];
- 在 ANGULARJS 中:s.Id as s.Name for s 在 Player 中显示 playerName i
- FB 连接:显示 response.name
- 使用jQuery突出显示效果时 window.name 的奇怪问题
- 如何使用 object.id 显示列表中的 object.name
- 以javascript显示触发鼠标事件的ELEMENT NAME
- 试图显示name angular
- Javascript / PHP显示id或name对象
- Highcharts工具提示格式化程序只显示this.point.name中的前8个字符
- AngularJs的interval只显示{{name}}
- DeviceInformation.name显示蓝牙版本而不是名称