我正在做一个基于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()
答案 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