我正在做一个基于卓别林样板的示例项目,并且工作正常,但我无法理解提取完成后视图如何呈现,例如您可以使用 Backbone 进行事件更改,或者在 fetch 方法中使用回调,但是对于卓别林,这是怎么做到的?,是否有使用 Backbone 的事件更改? 什么类别的卓别林绑定事件?如何进行绑定?
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()
在
执行@view = new CartView model: campaign
将campaign
对象作为模型分配给视图的位置。ChaplinJS View 类会自动执行以下操作:
if (target === 'model' || target === 'collection') {
prop = this[target];
if (prop) {
this.listenTo(prop, eventName, callback);
}
}
这应该回答你的问题
我会把你的获取逻辑放到控制器上:
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