Backbone.js 在初始化中读取模型属性
Backbone.js read model attributes in initialize
在我看来,我需要将一个属性值重新复制到另一个属性中。
这是我在 coffeescript 中的代码
class MyFactoryView extends Backbone.View
initialize: ->
@model.fetch reset: true
@model.set('NewStatus', @model.get('CurrentStatus'))
当我调用 get 时,这段代码抛出一个未定义的异常。
但是如果我做一个 console.log @model,我可以在属性中看到 CurrentStatus
。
由于 ,fetch
是异步的,您只能在完成后访问属性。
class MyFactoryView extends Backbone.View
initialize: ->
@model.fetch
reset: true
context: @
success ->
@model.set('NewStatus', @model.get('CurrentStatus'))
对于该特定行,使用模型的 parse
函数在同步时初始化 NewStatus
属性可能更简单。
对于控制台,查看 which explains that the console contains live references 并且不复制任何内容。
在我看来,我需要将一个属性值重新复制到另一个属性中。
这是我在 coffeescript 中的代码
class MyFactoryView extends Backbone.View
initialize: ->
@model.fetch reset: true
@model.set('NewStatus', @model.get('CurrentStatus'))
当我调用 get 时,这段代码抛出一个未定义的异常。
但是如果我做一个 console.log @model,我可以在属性中看到 CurrentStatus
。
由于 fetch
是异步的,您只能在完成后访问属性。
class MyFactoryView extends Backbone.View
initialize: ->
@model.fetch
reset: true
context: @
success ->
@model.set('NewStatus', @model.get('CurrentStatus'))
对于该特定行,使用模型的 parse
函数在同步时初始化 NewStatus
属性可能更简单。
对于控制台,查看