我正在尝试找出如何最好地创建一种方法来使用 Ember.js 基于属性拉取特定对象。
现在我的模型看起来像这样:
App.Resume = Ember.Object.extend()
App.Resume.reopenClass
store: {}
findAll: ->
arr = Ember.ArrayProxy.create()
if xhr
xhr.abort()
return
xhr = $.ajax(
url: '../json/cv.json'
dataType: 'json'
timeout: 10000
).done (response) =>
response.users.forEach (user, i) =>
cv = @findOne(user.personId)
cv.setProperties(user)
return
values = (values for keys, values of @store)
arr.set('content', values)
arr
findOne: (id) ->
cv = @store[id]
if not cv
cv = App.Resume.create
id: id
@store[id] = cv
cv
如果您查看完成回调中的循环,您会发现它正在使用 user.id 创建模型 - 还有一个 user.specialization 字段。我希望能够通过该字段进行过滤。
任何想法/帮助将不胜感激!
谢谢!
丰富
最佳答案
您可以在任何 Ember Enumerable
上使用 filterProperty
,例如 Array
或 ArrayProxy
。默认情况下它匹配存在。您还可以传入一个参数来匹配数组中的每个属性。您可以将其与计算属性配对以在 View 中绑定(bind)。
filtered: function() {
return this.get('products').filterProperty('outOfStock')
}.property('products')
查看此jsbin举个例子。
关于javascript - Ember.js - 如何过滤模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17376256/