使用骨干模型从 JSON 检索特定数据
我正在创建应用程序的客户端视图,并且需要帮助从 JSON 文件中检索特定数据。我使用 Backbone.js 和 Underscore.js 来实现这一点。
(function($) {
window.Node = Backbone.Model.extend({
getName: function(){
return this.get('Name');
}
});
window.Nodes = Backbone.Collection.extend({
model:Node,
url: '/packageview.json'
});
window.NodeView = Backbone.View.extend({
tagName: "div",
className: "package-template",
events:{
"click #display-name" : "displayname",
},
//.. I have a render and initialize function here which should not be a concern
displayname: function(){
var node = new Node();
alert(node.getName()); //trying to alert
},
});
});
我正在尝试从模型中获取名称并提醒它。我的 html 中有一个带有 id 的按钮,当我按下该按钮时,我会收到“未定义”警报。我的 JSON 文件如下所示:
{
"Id": 2,
"Name": "Some Package",
"IsComplete": false,
"IsNodeTagComplete": false
}
我认为我在某个地方犯了一个愚蠢的错误。我对模型的期望太高了吗?
I am creating a client view of an application and I need help with retrieving specific data from my JSON file. I am using Backbone.js along with Underscore.js to achieve this.
(function($) {
window.Node = Backbone.Model.extend({
getName: function(){
return this.get('Name');
}
});
window.Nodes = Backbone.Collection.extend({
model:Node,
url: '/packageview.json'
});
window.NodeView = Backbone.View.extend({
tagName: "div",
className: "package-template",
events:{
"click #display-name" : "displayname",
},
//.. I have a render and initialize function here which should not be a concern
displayname: function(){
var node = new Node();
alert(node.getName()); //trying to alert
},
});
});
I am trying to get the name from model and alert it. I have a button in my html with an id, and when I press that button I get "undefined" as an alert. Here is how my JSON file looks:
{
"Id": 2,
"Name": "Some Package",
"IsComplete": false,
"IsNodeTagComplete": false
}
I think I am making a silly mistake somewhere. Am I expecting way to much from model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里做的是这个
所以这就是我正在做的访问 JSON
这解决了我的目的,但不完全是在主干中使用 getter 的方式。
What I am doing here is this
So here is what I am doing to access the JSON
This solves my purpose but not quite the way getters would be used in backbone.
由于缺少很多上下文,我也可能犯了一个错误,但这是我的猜测 - 您正在创建一个空的节点模型。尝试在显示中执行类似的操作:
虽然这只是一种预感...也许我错过了您的上下文,但现在似乎是这种情况...
Since a lot of context is missing I too may be making a mistake but here's my guess - you are creating an empty Node Model. Try doing something like this in display:
This is just a hunch though...maybe I'm missing your context but this seems to be the case for now...