0

我正在做一个基于 Chaplin-boilerplate 的示例项目,并且工作完美,但我无法理解在获取完成后视图如何呈现,例如使用 Backbone 可以在事件更改时使用,或者使用回调fetch 方法,但是用 chaplinjs 是怎么做到的?,有没有使用 Backbone 的事件更改?,什么类的 Chaplinjs 绑定了事件?装订怎么样?

class CampaignController extends Chaplin.Controller

  title: 'Campaign'
    index: (params) ->
    campaign = new Campaign()
    @view = new CartView model: campaign


class CartView extends View
  template: template
  template = null
  container: '#cart'
  autoRender: true
  className: 'cart'
  initialize: ->
    super
  render: ->
    super

class Campaign extends Model

  initialize: (attributes, options) ->
    super
    @urlRoot = "http://localhost/store/js/test-data/cart.json"
    @fetch()
4

2 回答 2

2

@view = new CartView model: campaign正在将campaign对象作为模型分配给视图。ChaplinJS 视图类自动执行以下操作:

if (target === 'model' || target === 'collection') {
  prop = this[target];
  if (prop) {
    this.listenTo(prop, eventName, callback);
  }
}

这应该回答你的问题

于 2013-03-30T19:41:57.833 回答
0

我会将您的获取逻辑放到控制器中:

class Campaign extends Model
    urlRoot = "http://localhost/store/js/test-data/cart.json"

Campaign = require 'models/Campaign'
class CampaignController extends Chaplin.Controller

  title: 'Campaign'
    index: (params) ->
    campaign = new Campaign()
    campaign.fetch
       success:
          @view = new CartView model: campaign
       error:
           console.error('sorry dude')


class CartView extends View
  template: template
  template = null
  container: '#cart'
  autoRender: true
  className: 'cart'
  initialize: ->
    super
  render: ->
    super
于 2013-12-09T15:36:04.000 回答