所以,我从继承的项目获得了这个数据。将数据从JSON传递到主干>模板页面
- JSON数据的字段。
defaults:{
"coolstuff":{uuid: null},
"coolStartDate":new Date(),
"coolEndDate": new Date(),
"cooldata":'',
"supercool":'', // I am adding this (trying)
},
从这些其他一些相关的JS为:
offerStart: function() {
var date = this.get('coolStartDate')
return (_.isNull(date)) ? new Date() : helper.formatDate(new Date(date)) ;
},
其他一些数据被发现,被称为加价为模板内;
<%= cooldata %>
我在抓住'supercool'数据的每一次尝试都失败了。我已经尝试了不同的synatax,在页面上,离开页面,一切。
我想知道我在骨干做(我是新来的明显Backbone.js的)
为了使用我的新的数据块或数据字段“过冷”的通过JSON,并允许它在页面中作为模板工作。
在这种特殊情况下,一个下拉菜单。
<div class="form-group">
<select class="form-control filter">
<option><%= supercool %></option>
<option><%= supercool %></option>
</select>
</div>
更新!
这是我在我的Backbone.js的,但仍然失败第一次运行当前的尝试。
(1.) MODEL。 (型号/ page.js)
define([
'jquery',
'underscore',
'underscore', // Page > Model
'backbone',
'helpers/helpers',
'bbvalidation'
], function(_, Backbone, Helpers) {
var helper = new Helpers();
var offerModel = Backbone.Model.extend({
urlRoot: "/loyalty/api/supercoolfile",
idAttribute: 'uuid',
url: function() {
return this.urlRoot + '/coolguys/' + this.get("id"); //
},
defaults:{
"supercool": "", // here
},
(2.) VIEW。 (视图/仪表板/ page.js)
define([
'jquery',
'underscore', // Views -- js/views/page.js
'backbone',
'vm',
'text!templates/dashboard/page.html'
],
template = _.template(<'<p>Name: <%= supercool %> </p>'),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
});
(3.)拉低数据INTO TEMPLATE(尝试)/dashboard/page.html
<option><%= supercool %></option>
应该工作,对?他们不适合我。
男人啊!谢谢一堆;好吧,我的第一部分代码应该在我的Models文件中,对吗?我应该在'视图'中写什么 - 这就是你建议我弹出'视图'渲染的内容吗?我认为这可能是我的错误!明天当我回到应用程序时,我会明白的! –
是的,第一部分代码应该位于Backbone Model对象中,并且需要使用上面的render函数创建View Backbone对象。您将实例化视图并传入模型的实例,然后调用视图的渲染函数。我会看看一些示例代码,因为有很多东西可以让它们全部正常工作。 – EmptyArsenal
谢谢@EmptyArsenal我仍然在这里打墙;我只是更新我的问题,我知道如果我得到这个,我可以成功开始使用backbone.js! –