我跟随Ryan Bates在Backbone.js上的示例,使用Backbone + Eco开始一个项目。这很棒。但是,我试图显示嵌套属性。
例如,我正在尝试执行此操作:<%= @stream.user.get('name') %>
index.jst.eco
我得到Uncaught TypeError: Cannot call method 'get' of undefined
但是,我可以让<%= @stream.get('stream_type') %>
工作。
这是REST API数据:
[
: {
: : "id":"5004095283de4ca9ff000005",
: : "created_at":"2012-07-16T12:30:10Z",
: : "stream_type":"project",
: : "user":
: : {
: : : "id":"5002f30560de7d0ffb000003",
: : : "name":"Regular User2"
: : },
...
我也尝试使用Backbone.DeepModel
来扩展我的模型,但这似乎不起作用。
class Project1.Collections.Streams extends Backbone.Collection
url: '/streams'
model: Topaz.Models.Stream
class Project1.Models.Stream extends Backbone.DeepModel
这是我对该系列的看法,非常标准。
class Project1.Views.StreamsIndex extends Backbone.View
#views/streams/index.js
template: JST['streams/index']
initialize: ->
@collection.on('reset', @render, this)
@collection.on('add', @appendStream, this) #rerenders entire template
render: ->
$(@el).html(@template())
@collection.each(@appendStream)
this
appendStream: (stream) =>
view = new Topaz.Views.Stream(model: stream)
@$('#streams').append(view.render().el) # looks for the #entries element within the view’s element and not on the page directly
这是模型的视图
class Project1.Views.Stream extends Backbone.View
template: JST['streams/show']
className: 'stream-item'
initialize: ->
@model.on('change', @render, this) #The change event is triggered by Backbone when a model saves
render: ->
$(@el).html(@template(stream: @model))
this