【发布时间】:2015-01-17 06:44:51
【问题描述】:
我有两种过滤集合的方法:
方法1:当我将它传递给视图时,使用此方法失败。
byCategory = @projects.byCategory(data)
方法二:这个方法效果很好。
byCategory = new App.Collections.Projects(@projects.byCategory(data))
当我映射 byCategory 并获取标题时,这两种方法都有效:
byCategory.map (project) ->
console.log project.get('title')
当我将它传递给视图时,第二种方法有效。但是第一种方法失败了:
view = new App.Views.ProjectsCarousel(collection: byCategory)
$('.slides').html(view.render().el);
我的问题是:为什么?当我将它传递给视图时,为什么第一个失败,为什么第二个有效?第一个产生错误日志:TypeError: _ref is undefined
完整代码:
路由器
class App.Routers.PortfolioRouter extends Backbone.Router
routes:
'': 'index'
index: ->
controls = new App.Views.ProjectsControls({el: '#list', projects: new App.Collections.Projects()});
收藏
class App.Collections.Projects extends Backbone.Collection
url: '/de/projects'
byCategory: (cat) ->
return @where category: cat
观看次数
class App.Views.ProjectsControls extends Backbone.View
events:
'click a': 'selectCategory'
initialize: (options) ->
@projects = options.projects
@projects.fetch()
selectCategory: (event) ->
event.preventDefault()
data = $(event.currentTarget).attr('data-category')
# This method fails when I pass it to the view
# byCategory = @projects.byCategory(data)
# This method works very well, why does it work and the previous no?
byCategory = new App.Collections.Projects(@projects.byCategory(data))
# it works with both methods
byCategory.map (project) ->
console.log project.get('title')
view = new App.Views.ProjectsCarousel(collection: @projects)
$('.slides').html(view.render().el);
class App.Views.ProjectsCarousel extends Backbone.View
template: JST['projects/carousel']
render: ->
@$el.html(@template(projects: @collection))
this
模板
<% for project in @projects.models: %>
<%= project.get('title') %>
<% end %>
【问题讨论】:
-
您能给出
App.Collections.Project的定义吗?专门针对App.Collections.Project.byCategory() -
另外,您如何渲染
App.Views.ProjectsCarousel中的集合?
标签: javascript ruby-on-rails backbone.js coffeescript