javascript - Emberjs 中没有任何内容处理事件错误

标签 javascript json ember.js

我正在尝试使用 Emberjs 中的“filterProperty”来过滤 JSON 响应。但我收到此错误,未捕获错误:没有处理“最后”事件

这是我的 App.js

App = Ember.Application.create({});

App.IndexRoute = Ember.Route.extend({
    renderTemplate : function(controller) {
        this.render('MyApp', {
            controller : controller
        });
    },
    model : function() {
        return App.MyTemplateModel.find();
    }
});

App.IndexController = Ember.ArrayController.extend({
    last : (function() {
        this.get('content').filterProperty('last_name', 'Solow');
    }).property('content.@each.type')
});

App.MyTemplateModel = Ember.Model.extend({
    id : Ember.attr(),
    last_name : Ember.attr(),
    first_name : Ember.attr(),
    suffix : Ember.attr(),
    expiration : Ember.attr()
});

App.SiteController = Ember.ObjectController.extend({

});

App.MyTemplateModel.url = "http://ankur1.local/index.php/api/example/users/";
App.MyTemplateModel.adapter = Ember.RESTAdapter.create();
var existing = App.MyTemplateModel.find();
App.MyTemplateModel.camelizeKeys = true;

这是我的 HTML 页面,

<script type="text/x-handlebars" data-template-name="MyApp">

            {{#each item in content }}
            <tr><td>
            {{id}} <p> {{item.first_name}} {{item.expiration}}</p>
            </td></tr>
            {{/each}}

            <button {{action last}}>filter</button>

        </script>

        <script type="text/x-handlebars">
            <h1>Application Template</h1>
            {{outlet}}
        </script>

    </body>

我在 App.js 中可能做错了什么,或者我应该使用任何其他属性来过滤 JSON 响应吗?

最佳答案

您最后在 IndexController 上将该属性声明为计算属性,但如果您想使用{{action}} 帮助器,则不允许这样做。它是一个简单的函数。这就是为什么 Ember 在任何地方都找不到合适的事件并提示它。

App.IndexController = Ember.ArrayController.extend({
    // for initial filling of this property, will be overridden by last action
    filteredContent : Ember.computed.oneWay("content"), 
    last : function() {
        var filtered = this.get('content').filterProperty('last_name', 'Solow');
        this.set("filteredContent", filtered);
    }
});

<script type="text/x-handlebars" data-template-name="MyApp">

{{#each item in filteredContent }}
  <tr><td>
  {{id}} <p> {{item.first_name}} {{item.expiration}}</p>
  </td></tr>
{{/each}}

<button {{action last}}>filter</button>

</script>

所以我基本上做了两件事:

  1. 我将计算属性更改为普通函数。
  2. 模板正在迭代filteredContent而不是内容。(请注意我必须在您的 Controller 上进行的初始化。)

所以基 native 制是在 Controller 上有一个附加属性,它保存过滤后的内容。您必须对此进行扩展,因为您的用例肯定会更复杂一些。 :-)

关于javascript - Emberjs 中没有任何内容处理事件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18089982/

相关文章:

json - 删除 Lambda 支持的自定义资源的支持 Lambda 函数

Ember.js、EmberCLI - 从 URL 中删除哈希 (#)

javascript - Ember.js 在 View 中获取模型

javascript - 数字输入不会将 0 作为 React 应用程序中的值

javascript - jQuery Fade Slider - 删除定义的高度?

javascript - IE 11、10 和 9 中的图像缩放很糟糕

javascript - 通过 post 传递 json 对象以及其他数据

javascript - 每个循环从 jQuery 创建 JSON

ember.js - 观察非 ember 全局变量

javascript - 在 AngularJS 中使用模板和过滤