使用更多上下文编辑。当$ CONSTRUCTOR = passed_in时,它会在Firefox和Chrome中抛出“passed_in不是构造函数”。 $ CONSTRUCTOR = not_passed_in时,它不是
initialize: (options) ->
@collection.bind 'all', @render
$('.search_box').parent('form').submit (event) =>
@loading()
event.preventDefault()
query = $(event.target).find('.search_box').val()
window.app.navigate('?query=' + query, trigger: true)
passed_in = PaginationView
@render(passed_in)
render: (passed_in)=>
if @collection.isEmpty() && @collection.query
@$el.html(JST['users/no_results'](query: @collection.query))
else if @collection.isEmpty() # Not loaded yet
@$el.html("<div class='loading'></div>")
else
html = JST['users/user_list'](@viewData())
@$el.html(html)
for user in @collection.models
html = new UserListItemView(model: user).render().el
@$('tbody.users').append(html)
not_passed_in = PaginationView
new $CONSTRUCTOR(
type: "user"
el: @$('.paginate')
model: @collection
data: {}
onError: @onError
).bind('change', @loading)
这是CoffeeScript的已知(错误|功能)吗?或者我做错了什么?
(这些在Backbone.coffee视图中。我怀疑这是相关的)
答案 0 :(得分:0)
简化问题:
MyView = Backbone.View.extend
initialize: (options) ->
passed_in = PaginationView
@render(passed_in)
render: (passed_in) =>
not_passed_in = PaginationView
new passed_in(
type: "user"
el: @$('.paginate')
model: @collection
data: {}
onError: @onError
).bind('change', @loading)
1)通常@render的最后一行是@
或this
而不是另一个视图。
2)最有可能的问题是有一些其他代码在没有参数的情况下调用@render()
。建议你给出一个默认参数:
render: (passed_in=PaginationView) =>
3)如果PaginationView
创建了MyView
,则可能会出现堆栈溢出错误:
4)JSON.stringify在passed_in
随时被调用吗?如果是这样,它会将函数转换为undefined
:
passed_in = JSON.stringify(passed_in)
undefined == passed_in
# always true if passed_in is a function