- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很困惑 - 我以为我的模型绑定(bind)工作正常,但它只是作为带有伪造的 ajax 请求的 jsFiddle 。我已将模型绑定(bind)到 View ,如果我重写 .fetch() 并伪造响应,则一切正常(我可以更新页面上的模型和 View 更新)。但是,当我不覆盖 .fetch()
并使用 urlRoot
参数并等待响应时,我会收到错误。 p>
这是工作中的 jsFiddle,其中在使用伪造的响应调用 .fetch()
后渲染模型,然后进行更改:
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
最佳答案
这里发生了很多奇怪和困惑的事情。不要害怕,一切还没有消失,困惑是可以解决的。
Backbone 的fetch
应该返回一个jqXHR
,而不是模型本身。您的 fetch
实现错误地返回 @parse resp
和您的 parse
返回 @
(这也是不正确的,有时两个错误确实会产生一个正确的结果)。结果是这样的:
App.Data.thing = new App.Models.Thing(id: 1).fetch()
在您使用 fetch
时为您提供有用的 App.Data.thing
,但它与 Backbone 的 fetch
不符。因此,您的 fetch
已损坏,并且您没有正确使用 fetch
;然后你尝试将jqXHR
作为模型提供给你的 View ,并且你的 View 在jqXHR
而不是模型上设置@view
:
initialize: (options) ->
#...
@options.model.view = @
因此,您最终会在 jqXHR
上得到 view
属性,但模型没有 @view
(因为 App.Data .thing
不是模型),并且您在模型的更改处理程序中收到“无法调用未定义的‘render’方法”错误。
你应该这样做:
App.Data.thing = new App.Models.Thing(id: 1)
App.Data.thing.fetch()
现在开始您的解析
:
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 @
@
来自fine manual :
parse is called whenever a model's data is returned by the server, in fetch, and save. The function is passed the raw
response
object, and should return the attributes hash to be set on the model.
因此 parse
只是应该将服务器的响应调整为 set
在模型上。您的 parse
正在设置属性并返回 @
。结果是您最终将执行与 m.set(m)
相同的操作。因此,摆脱您的 parse
实现,您的实现是不正确的,您甚至不需要它。
您的模型/ View 连接是向后的: View 引用模型,模型不引用 View 。你的模型中有这个:
initialize: (item) ->
console.log 'App.Models.Thing init'
@bind 'change', ->
console.log 'a Thing has changed, update ItemView!'
@view.render()
您认为:
initialize: (options) ->
console.log 'App.ItemViews.Thing init'
# Bind
@options.model.view = @
您应该在创建模型时将模型传递给 View (您就是这样,所以这没问题):
App.Rendered.thingView = new App.ItemViews.Thing(model: App.Data.thing)
然后 View 应该绑定(bind)到模型:
initialize: (options) ->
@model.on('change', @render)
您可以在模型中删除initialize
实现。
此外,您可以(并且应该)直接在命名空间中声明类,但不要这样做:
class Thing extends Backbone.Marionette.ItemView
#...
App.ItemViews.Thing = Thing
这样做:
class App.ItemViews.Thing extends Backbone.Marionette.ItemView
#...
此外,setTimeout
的字符串/评估形式是邪恶的,几乎不应该被使用。不要这样做:
setTimeout 'App.updateModel()', 2000
这样做:
setTimeout (-> App.updateModel()), 2000
可能还有更多,但希望这能让您开始并解决您眼前的问题。
关于javascript - 重写 .fetch() 可处理虚假数据、urlRoot 错误和实际模型 ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10691860/
这些是我的服务器的路径 URL: GET /watchlists GET /watchlists/:id POST /watchlists // Create a new watchlists PUT
当我尝试覆盖模型的 urlRoot 属性并对该模型进行提取时,urlRoot 实际上将整个函数作为字符串返回。下面是重写的 urlRoot 的样子: urlRoot: function() { r
我无法理解 urlRoot 在 Backbone.js 中的工作原理。我正在尝试从 NodeJS/MongoDB 后端获取具有特定 _id 的任务。 我不太清楚如何将 id 属性传递到 URI 中。我
地点:htdocs -api.roamingcamper(RESTful + CodeIgnter) - Backbone Site = Backbone.Model.extend({ urlRoot
假设我有一个 Person 模型/集合,如下所示; define([ 'base/BaseModel' ], function(BaseModel) { var Person = Ba
我需要在运行时将 urlRoot 传递给模型,因为模型的几个不同类使用不同的 urlRoot。 这是我的模型: App.Models.Table = Backbone.Model.extend({ }
使用 backbone.js 我有一个特殊情况,我需要在创建模型时将实际的 urlRoot 动态传递给模型。 这是我尝试过但没有运气的方法: var reward = new Reward({url
我很困惑 - 我以为我的模型绑定(bind)工作正常,但它只是作为带有伪造的 ajax 请求的 jsFiddle 。我已将模型绑定(bind)到 View ,如果我重写 .fetch() 并伪造响应,
我是 backbone.js 的新手,正在开发一个基于 node.js 的 Backbone 前端和后端示例项目。我有两个实体:后端中的 User 和 Bank 以及与这两个具有相同名称的模型对应的
我正在做一些练习来熟悉 Backbone。我正在尝试使用 Model 的 urlRoot 属性从我的服务器获取 JSON 文件。 当我输入 urlRoot: "./js/json/todo" 时,我收
我是一名优秀的程序员,十分优秀!