【问题标题】:Ember js Mixin Not Compiling Grouping ResultEmber js Mixin未编译分组结果
【发布时间】:2015-11-17 02:08:09
【问题描述】:

我使用的是 Ember App Kit 而不是 ember -cli

我从服务器收到了一些响应,例如 [{date: '2014-01-15T16:22:16-08:00', message: 'Tidal wave occurred'}, {date: '2014-01-15T05:00:16-08:00', message: 'Tornado destroyed town'}, {date: '2014-01-13T14:22:16-08:00', message: 'Volcanic eruption, likely'}, {date: '2014-01-13T16:24:16-08:00', message: 'Ice shelf calving off completely'}, {date: '2014-01-11T11:27:26-08:00', message: 'Mother-in-law visiting'}]

我正在尝试按日期对其进行分组,以便输出看起来像

`Today
----------
4:22 pm - Tidal wave occurred
5:00 am - Tornado destroyed town

Monday
----------
2:22 pm - Volcanic eruption, likely

这是我的模型应用程序/模型

`Model = DS.Model.extend

  date: DS.attr "string"
  message: DS.attr "string"


`export default Model`

这是我的路线 --app/routes/test

Route = Ember.Route.extend 

  model: () ->
    return @store.find "my-model"

`export default Route`

这是我按日期分组的 mixin --app/mixin/groupable-mixin.coffee

  Mixin = Ember.Mixin.create
  group: null
  ungroupedContent: null

  groupedContent: (->
    model = @
    groupedContent = Ember.A([])

    groupCallback = @get('group')
    ungroupedContent = @get('ungroupedContent')

    return groupedContent unless groupCallback
    return groupedContent unless ungroupedContent

    ungroupedContent.forEach (item) ->
      group = groupCallback.call(model, item)
      return unless groupKey = group.get('key')

      foundGroup = groupedContent.findProperty('group.key', groupKey)

      unless foundGroup
        foundGroup = groupedContent.pushObject Ember.ArrayProxy.create
          group: group,
          content: Ember.A([])

      foundGroup.get('content').pushObject(item)

    groupedContent
  ).property('group', 'ungroupedContent.@each')

  `export default Mixin`

这是我的控制器 app/controller/test.coffee

    'import GroupMixin from "app/mixins/groupable-mixin" '

Controller = Ember.ArrayController.extend GroupMixin,
  ungroupedContentBinding: 'content' # tell Groupable where your default content is

  # the callback that will be called for every
  # item in your content array -
  # just return the same 'key' to put it in the same group
  group: (activity) ->
    Ember.Object.create
      key: moment.utc(activity.get('date')).format('YYYY-MM-DD') # using momentjs to pluck the day from the date
      description: 'some string describing this group (if you want)'
# Controller = Em.ArrayController.extend()


`export default Controller`

这是我的模板应用程序/模板/test.hbs

{{#each groupedContent}}
  {{group.key}} - {{group.description}}

  {{#each content}}
   {{message}}
  {{/each}}
{{/each}}

但是,当我做grunt server 时,我得到以下回复

Warning: Error compiling tmp/javascript/app/mixins/groupable-mixin.js Used --force, continuing.

当我这样做时grunt server --force 我看到以下内容

如何解决这个问题。我是否正确使用了 mixin。我是否在控制器中正确导入了 mixin?

还有如何对日期进行排序,以便先显示最新的日期,然后显示较旧的日期。

【问题讨论】:

    标签: javascript date ember.js coffeescript ember-data


    【解决方案1】:

    尝试在导入语句中使用反引号而不是单引号。

    `import GroupMixin from "app/mixins/groupable-mixin"`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-17
      • 2015-09-24
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2014-07-01
      • 1970-01-01
      相关资源
      最近更新 更多