Backbone.js Can’;t第二次实例化主干视图
由于某些原因,我不能在我的主干应用程序中多次实例化视图。我得到的Backbone.js Can’;t第二次实例化主干视图,backbone.js,coffeescript,requirejs,Backbone.js,Coffeescript,Requirejs,由于某些原因,我不能在我的主干应用程序中多次实例化视图。我得到的未定义不是一个函数。为什么会这样 应用程序咖啡 define [ 'views/start' ], ( StartView ) -> initialize = -> # Take us to the start view startView = new StartView() startView.render() # Works just fine initialize
未定义不是一个函数
。为什么会这样
应用程序咖啡
define [
'views/start'
], (
StartView
) ->
initialize = ->
# Take us to the start view
startView = new StartView()
startView.render() # Works just fine
initialize: initialize
define [
'jquery'
'underscore'
'backbone'
'text!templates/start.html'
'views/question'
], (
$
_
Backbone
StartTemplate
QuestionView
) ->
StartView = Backbone.View.extend
events:
'click #btn-begin': 'pushQuestionView'
render: ->
# Create a template and stuff it into the view element
@$el.html @template = _.template StartTemplate
# Inject the HTML in the document
$('#main').html @el
# Return view for chaining
@
pushQuestionView: ->
questionView = new QuestionView()
questionView.render()
StartView
define [
'jquery'
'underscore'
'backbone'
'text!templates/question.html'
'views/form'
'views/start'
], (
$
_
Backbone
QuestionTemplate
FormView
StartView
) ->
QuestionView = Backbone.View.extend
events:
'click #btn-prev': 'goBack'
render: ->
# Create a template and stuff it into the view element
@$el.html @template = _.template QuestionTemplate
# Inject the HTML in the document
$('#main').html @$el
# Return for chaining
@
goBack: ->
console.log StartView # => undefined
startView = new StartView() # => Uncaught TypeError: undefined is not a function
startView.render()
QuestionView
浏览/开始。咖啡
define [
'views/start'
], (
StartView
) ->
initialize = ->
# Take us to the start view
startView = new StartView()
startView.render() # Works just fine
initialize: initialize
define [
'jquery'
'underscore'
'backbone'
'text!templates/start.html'
'views/question'
], (
$
_
Backbone
StartTemplate
QuestionView
) ->
StartView = Backbone.View.extend
events:
'click #btn-begin': 'pushQuestionView'
render: ->
# Create a template and stuff it into the view element
@$el.html @template = _.template StartTemplate
# Inject the HTML in the document
$('#main').html @el
# Return view for chaining
@
pushQuestionView: ->
questionView = new QuestionView()
questionView.render()
StartView
define [
'jquery'
'underscore'
'backbone'
'text!templates/question.html'
'views/form'
'views/start'
], (
$
_
Backbone
QuestionTemplate
FormView
StartView
) ->
QuestionView = Backbone.View.extend
events:
'click #btn-prev': 'goBack'
render: ->
# Create a template and stuff it into the view element
@$el.html @template = _.template QuestionTemplate
# Inject the HTML in the document
$('#main').html @$el
# Return for chaining
@
goBack: ->
console.log StartView # => undefined
startView = new StartView() # => Uncaught TypeError: undefined is not a function
startView.render()
QuestionView
观点/问题。咖啡
define [
'views/start'
], (
StartView
) ->
initialize = ->
# Take us to the start view
startView = new StartView()
startView.render() # Works just fine
initialize: initialize
define [
'jquery'
'underscore'
'backbone'
'text!templates/start.html'
'views/question'
], (
$
_
Backbone
StartTemplate
QuestionView
) ->
StartView = Backbone.View.extend
events:
'click #btn-begin': 'pushQuestionView'
render: ->
# Create a template and stuff it into the view element
@$el.html @template = _.template StartTemplate
# Inject the HTML in the document
$('#main').html @el
# Return view for chaining
@
pushQuestionView: ->
questionView = new QuestionView()
questionView.render()
StartView
define [
'jquery'
'underscore'
'backbone'
'text!templates/question.html'
'views/form'
'views/start'
], (
$
_
Backbone
QuestionTemplate
FormView
StartView
) ->
QuestionView = Backbone.View.extend
events:
'click #btn-prev': 'goBack'
render: ->
# Create a template and stuff it into the view element
@$el.html @template = _.template QuestionTemplate
# Inject the HTML in the document
$('#main').html @$el
# Return for chaining
@
goBack: ->
console.log StartView # => undefined
startView = new StartView() # => Uncaught TypeError: undefined is not a function
startView.render()
QuestionView
因为它是第一次工作,所以不可能是语法错误。您指的是
start
中的question
和question
中的start
。它形成循环依赖。这就是这个问题的原因
来自文件
如果定义了循环依赖项(a需要b,b需要a),则在
在这种情况下,当调用b的module函数时,它将得到一个未定义的
a的价值
这似乎不对;相对于
main.coffee
的文件路径是views/start
。我现在试着把它改成你的建议,结果requirejs抛出了一个404.oh。很抱歉我想这应该是一条上升的路。你能试试吗?。/start是的。我错了。它应该是views/start
。你说得对。但你还有其他问题,这确实是问题所在。我用StartView=require('views/start')
修复了它。谢谢在获得未捕获类型错误:
之前,您在控制台中是否遇到任何错误?可能是您在开始中引用了问题
,在问题
中引用了开始
。这可能就是问题的原因。没有其他错误。我指的确实是《开始》中的问题,以及《开始》中的问题。这通常是个问题吗?是的。这就是问题所在。请参阅我的最新答案。