私は混乱しています-モデルバインディングが正しく機能していると思いましたが、それは偽のajaxリクエストを伴うjsFiddleとしてのみでした。モデルをビューにバインドしました.fetch()
。応答をオーバーライドして偽造すると、すべてが機能します(モデルを更新し、ページでビューを更新できます)。ただし、パラメータを上書きして使用せずに応答を待つと、エラーが発生します。.fetch()
urlRoot
.fetch()
これは、変更されたものではなく、偽の応答で呼び出した後にモデルがレンダリングされる、動作中のjsFiddleです。
http://jsfiddle.net/franklovecchio/FkNwG/182/
したがって、サーバー側でAPI呼び出しがある場合:
/thing/:id
の応答例/thing/1
:
{"id":"1","latitude":"lat1","longitude":"lon1"}
そして私はコメントアウトし.fetch()
ます、私はコンソールエラーを受け取ります:
load js core functions core.js:2
init model timeout app.js:114
initializer callback for history, routes app.js:95
App.Layouts.MyLayout onShow app.js:41
App.Regions.MyRegion onShow app.js:25
App.Models.Thing init app.js:55
App.ItemViews.Thing init app.js:87
Uncaught TypeError: Object #<Object> has no method 'toJSON' backbone.marionette-0.8.1.min.js:9
Parsing App.Models.Thing.fetch() response: {"id":"1","latitude":"lat1","longitude":"lon1"} app.js:62
Thing: {"id":"1","latitude":"lat1","longitude":"lon1"} app.js:66
a Thing has changed, update ItemView! app.js:57
Uncaught TypeError: Cannot call method 'render' of undefined app.js:58
update model app.js:108
Uncaught TypeError: Object #<Object> has no method 'set'
.fetch()
コメントアウトされたコード:
window.App = { }
window.App.Regions = { }
window.App.Layouts = { }
window.App.Models = { }
window.App.ItemViews = { }
window.App.Rendered = { }
window.App.Data = { }
# ----------------------------------------------------------------
# App.Regions.MyRegion
# ----------------------------------------------------------------
class MyRegion extends Backbone.Marionette.Region
el: '#myregion'
onShow: (view) ->
console.log 'App.Regions.MyRegion onShow'
App.Regions.MyRegion = MyRegion
# ----------------------------------------------------------------
# App.Layouts.MyLayout
# ----------------------------------------------------------------
class MyLayout extends Backbone.Marionette.Layout
template: '#template-mylayout'
regions:
contentRegion: '#content'
anotherRegion: '#another'
onShow: (view) ->
console.log 'App.Layouts.MyLayout onShow'
App.Layouts.MyLayout = MyLayout
# ----------------------------------------------------------------
# App.Models.Thing
# ----------------------------------------------------------------
class Thing extends Backbone.Model
urlRoot: () ->
'/thing'
initialize: (item) ->
console.log 'App.Models.Thing init'
@bind 'change', ->
console.log 'a Thing has changed, update ItemView!'
@view.render()
parse: (resp) ->
console.log 'Parsing App.Models.Thing.fetch() response: ' + JSON.stringify resp
@attributes.id = resp.id
@attributes.latitude = resp.latitude
@attributes.longitude = resp.longitude
console.log 'Thing: ' + JSON.stringify @
@
# If I don't override, I get an error.
###fetch: () ->
console.log 'override ajax for test - App.Models.Thing.fetch()'
resp =
id: 1
latitude: 'lat1'
longitude: 'lon1'
console.log 'Faked Thing response: ' + JSON.stringify resp
@parse resp###
App.Models.Thing = Thing
# ----------------------------------------------------------------
# App.ItemViews.Thing
# ----------------------------------------------------------------
class Thing extends Backbone.Marionette.ItemView
template: '#template-thing'
initialize: (options) ->
console.log 'App.ItemViews.Thing init'
# Bind
@options.model.view = @
App.ItemViews.Thing = Thing
# ----------------------------------------------------------------
# App.MyApp ...the Marionette application
# ----------------------------------------------------------------
App.MyApp = new Backbone.Marionette.Application()
# ----------------------------------------------------------------
# App.MyApp before init
# ----------------------------------------------------------------
App.MyApp.addInitializer (data) ->
console.log 'initializer callback for history, routes'
App.Rendered.myRegion = new App.Regions.MyRegion
App.Rendered.myLayout = new App.Layouts.MyLayout
App.Rendered.myRegion.show App.Rendered.myLayout
# GET thing
App.Data.thing = new App.Models.Thing(id: 1)
.fetch()
App.Rendered.thingView = new App.ItemViews.Thing(model: App.Data.thing)
App.Rendered.myLayout.contentRegion.show App.Rendered.thingView
# ----------------------------------------------------------------
# Test
# ----------------------------------------------------------------
App.updateModel = ->
console.log 'update model'
# Update the Thing with id = 1
App.Data.thing.set
latitude: 'somenewlat'
App.updateModelTimeout = ->
console.log 'init model timeout'
setTimeout 'App.updateModel()', 2000
App.updateModelTimeout()
$ ->
data = { }
App.MyApp.start data