简体   繁体   English

除了在javascript控制台中,rails on collection上的骨干不起作用

[英]backbone on rails collection fetch does not work except in javascript console

Update: 更新:

Forget about the first attempt, in my "another attempt", it works with firefox but fails in google chrome. 忘记第一次尝试,在我的“另一次尝试”中,它适用于Firefox,但在谷歌浏览器中失败。 The reason seems to be the alert pop up in firefox allows enough time for the collection to fetch the data, it returns 0 again (instead of 2) after I remove the alert. 原因似乎是firefox弹出警报允许集合有足够的时间来获取数据,在我删除警报后它再次返回0(而不是2)。 It seems the on('reset',.. ) is bypassed 似乎on('reset',..)被绕过了


I am new to backbone and was testing things out so I have 我是骨干新手,正在测试我所拥有的东西

collection file: 集合文件:

class Blog.Collections.Posts extends Backbone.Collection

  model: Blog.Models.Post

  url: '/bb/posts/list_posts'

models file: 模型文件:

class Blog.Models.Post extends Backbone.Model

router file: 路由器文件:

class Blog.Routers.Posts extends Backbone.Router
  routes: 'posts/list' : 'list'

  list: ->
    @collection = new Blog.Collections.Posts()
    @collection.fetch success: ->
       alert @collection
       view = new Blog.Views.PostsIndex()
       $('#center').html(view.render(posts: @collection).el)

view file: 查看文件:

 class Blog.Views.PostsIndex extends Backbone.View

   template: JST['posts/index']


   render: (posts) -> 
      $(@el).html(@template(posts))
      this

template file just displays the length of the posts. 模板文件只显示帖子的长度。 So when the collection fetches the json from the /bb/posts/list_posts url, I should wait for it to finish with success. 所以当集合从/ bb / posts / list_posts url中获取json时,我应该等待它成功完成。 But somehow the alert returns undefined and template renders length 0 但不知何故,警报返回undefined ,模板呈现长度0

Another attempt: 另一种尝试:

router file: 路由器文件:

class Blog.Routers.Posts extends Backbone.Router
  routes: 'posts/list' : 'list'

  list: ->
    @collection = new Blog.Collections.Posts()
    @collection.fetch()
    view = new Blog.Views.PostsIndex(collection: @collection)
    $('#center').html(view.render().el)

view file: 查看文件:

 class Blog.Views.PostsIndex extends Backbone.View

   template: JST['posts/index']

   initialize: ->
     @collection.on('reset',@render,this)
     alert @collection.length

   render: -> 
     $(@el).html(@template(posts: @collection))
     this 

this time, I get the alert @collection to return Object[Object] but it has length 0. 这次,我得到警告@collection返回Object [Object],但它的长度为0。

The fetch works FINE in javascript console and length is 2. Why doesn't it work? fetch在javascript控制台中运行FINE,长度为2.为什么它不起作用?

Found the solution here 在这里找到解决方案

Backbone.js - fetch method does not fire reset event Backbone.js - fetch方法不会触发重置事件

As it turns out, the problem being the 'reset' event was never fired. 事实证明,“重置”事件的问题从未被解雇过。 As is pointed out in the link, instead of fetch(), fetch({reset:true}) solves the problem. 正如链接中指出的那样,fetch({reset:true})解决了问题,而不是fetch()。 : ) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM