【发布时间】:2014-06-13 07:36:51
【问题描述】:
我正在使用 Backbone.js 和 Ruby on Rails 来呈现一组帖子,但屏幕上没有呈现任何内容。
应用文件
#= require_self
#= require_tree ./templates
#= require_tree ./models
#= require_tree ./views
#= require_tree ./routers
window.IntroProject =
Models: {}
Collections: {}
Routers: {}
Views: {}
init: ->
window.router = new IntroProject.Routers.HomepageRouter({})
Backbone.history.start()
$ ->
IntroProject.init()
模型和集合
class IntroProject.Models.Post extends Backbone.Model
paramRoot: 'post'
class IntroProject.Collections.PostsCollection extends Backbone.Collection
model: IntroProject.Models.Post
url: '/'
路由器
class IntroProject.Routers.HomepageRouter extends Backbone.Router
initialize: (options) ->
@posts = new IntroProject.Collections.PostsCollection()
@posts.fetch()
routes:
'':'index'
index: ->
@view ||= new IntroProject.Views.Posts.IndexView(collection: @posts)
@view.render()
查看
IntroProject.Views.Posts ||= {}
class IntroProject.Views.Posts.IndexView extends Backbone.View
el: '#posts'
template: _.template( $('#home-post-template').html() ) if $("#home-post-template").length
render: ->
@$el.append( @template() )
@
# _.each(@collection, (post) =>
# postHtml = $(@template(post))
# @$el.append( postHtml )
# )
# @
模板
<script type="text/template" id="home-post-template">
<div class="row">
<div class="small-12 columns">
<h2 class="blog-post-title"><%= post.title %></h2>
<small>
<p class="blog-post-meta">Published on <%= post.created_at.strftime('%B %d, %Y') %>
<% if (post.user.email) %>
by <strong><%= post.user.email %></strong>
<% end %>
</p>
</small>
<hr>
<div class="blog-post-body">
<p><%= post.body.html_safe %></p>
</div>
</div>
</div>
</script>
我可以在加载页面时调用render(),因为我执行了console.log('hello')。我什至也做了console.log(@collection)。输出:
PostsCollection {models: Array[0], length: 0, _byId: Object, constructor: function, model: function…}
_byId: Object
_idAttr: "id"
length: 2
models: Array[2]
__proto__: ctor
所以他们的帖子在集合中。
当我执行@$el.append( @template() ) 时,它说post 未定义,所以我执行了@$el.append( @template(post: @collection.models)。但帖子未定义。
总的来说,我了解发生了什么,但它不起作用。我错过了一些重要的东西。这里是my github repo
更新:
答案建议的更改
路由器
class IntroProject.Routers.HomepageRouter extends Backbone.Router
initialize: (options) ->
@posts = new IntroProject.Collections.PostsCollection()
@posts.fetch({ reset : true })
routes:
'':'index'
index: ->
@view ||= new IntroProject.Views.Posts.IndexView(collection: @posts)
查看
IntroProject.Views.Posts ||= {}
class IntroProject.Views.Posts.IndexView extends Backbone.View
el: '#posts'
template: _.template( $('#home-post-template').html() ) if $("#home-post-template").length
initialize: ->
@listenTo(@collection, 'reset', @render)
render: ->
_.each(@collection, (post) ->
postHtml = @template( post )
@$el.append( postHtml )
, @)
模板,我从post.title 中删除了post
现在我通过控制台收到Uncaught ReferenceError: title is not defined。
【问题讨论】:
-
可能是
@posts.fetch()函数。尝试在该函数中实现success回调。使用回调获取示例:stackoverflow.com/questions/19713613/… -
对不起,我刚刚注意到一个错误,应该是
@collection.each(function(post){ ... });,你需要在你的帖子中调用.toJSON():postHtml = @template( post.toJSON() ),看这个基本的例子:jsbin.com/musupufa/2/edit?html,js,output
标签: javascript ruby-on-rails ruby backbone.js coffeescript