1

我按照 Ryan Bates 在 Backbone.js 上的示例使用 Backbone + Eco 开始了一个项目。这很棒。但是,我一直在尝试显示嵌套属性。例如,我正在尝试这样做:<%= @stream.user.get('name') %>in index.jst.eco and I'm gettingUncaught 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
4

0 回答 0