在获取后,chaplin如何呈现模型?,视图如何知道获取完成后?

时间:2012-09-21 15:00:02

标签: javascript backbone.js chaplinjs

我正在做一个基于Chaplin-boilerplate的示例项目,并且工作正常,但我不能理解在fetch完成后视图的呈现方式,例如使用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()

2 个答案:

答案 0 :(得分:2)

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

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

这应该回答你的问题

答案 1 :(得分: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