javascript - Ember 路由或 Controller 将数据模型公开为 json

标签 javascript json model-view-controller ember.js

我正在尝试使用 Ember.js

我可以通过路由或 Controller 将我的数据模型公开为 JSON 吗?

我在商店中保存了一个这样的对象:

this.store.createRecord('Person', {
  id: 1,
  name: this.get('name'),
  email: this.get('email')
});

我想将来自路由或 Controller 的数据作为 JSON 对象公开。我不想使用任何 View 。

可以这样做吗? 感谢您的帮助!

编辑 我的路线是:

App.ResultRoute = Ember.Route.extend({
model: function() { 
   return this.store.find('person', 1);
   }
});

有“1”,因为我只想要这条记录。 这样它就起作用了,我在 View 中看到了 Person 对象的 {{name}} 和 {{email} 。 我只想查看 JSON,我尝试按照您的建议操作:

App.ResultRoute = Ember.Route.extend({
  afterModel: function (model) {
  model.get('content').forEach(function (item) {
    console.log(item.get('content'));
   });
 } 
});

但我收到此错误:

未捕获错误:断言失败:错误:传递的上下文对象多于路由的动态段:错误

我的错误是什么?

最佳答案

我这样做的方法是,我的模型中有一个 api,它会向任何提出请求的人返回一个普通的 json 对象。所以Person模型将有 getPersonDetails方法将隐藏所有内部细节,包括属性和关联以及其他任何内容,并返回 person 的状态调用它的对象。 因此,例如,如果您想显示人员表格或其他内容,您可以执行 createRecord,然后只询问新创建的 person对象的详细信息。

关于javascript - Ember 路由或 Controller 将数据模型公开为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24396494/

相关文章:

javascript - jquery/javascript/html/css 中调用的弹出框/吸尘器框是什么

javascript - 从 JSON 数据输出元素的递归函数

asp.net-mvc - Controller.Json 方法将 & 编码为\u0026

reactjs - react : axios post request with both params and body

unit-testing - 使用 Gallio 运行 NUnit 测试

javascript - "Interpolate"不是一个函数

javascript - javascript 中是否有 'OrElse' 等效项?

javascript - 是否可以使用 CSS 创建 flex 的虚线?

java - Gson.toJson( )"Exception java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits"

javascript - Backbone.js - 在哪里定义 View 助手?