0

我是ember.js新手,面临日常问题:-)

今天的问题是我的服务器后端不支持sideloading。(如果我理解“sideload”正确,它是服务器一次返回具有多个 JSON 根的相关模型)

例如,我的服务器仅在每个请求以下返回:

为了/api/v1/posts.json

{
  posts: [{
    id: 91,
    title: "ember.js"
  }, {
    id: 81,
    title: "ember-data.js"
  }]
}

为了/api/v1/comments.json

{
  comments: [{
    id: 928,
    postId: 91,
  }, {
    id: 927,
    postId: 92,
  }]
}

当我加载post模型时, go /#/posts,视图没有呈现comment,但是如果我通过在位置栏中comment键入 URL 手动加载,然后返回,评论会正确显示。comment route/#/comments

所以我只是尝试这样,在post route,(第三行)

App.PostsRoute = Ember.Route.extend({
  model: function(params) {
    this.store.find('comment');
    return this.store.find('post');
  }
})

它有效!(也加载和填充评论!)但我认为这不是正确的方法。

有什么好的或正确的方法可以做到这一点?

编辑——添加模型定义

我的帖子模型类似于:

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  content: DS.attr('string'),
  comments: DS.hasMany('comment', {async: true}),
})

和评论模型是:

App.Account = DS.Model.extend({
  content: DS.attr('string'),
  post: DS.belongsTo('post'),
})

我不使用ember-cliember-starter-kit从主页和网络上观看一些教程。

而且,我使用自定义Adapter是因为我的后端实际上不支持 ember 样式的 JSON 响应,我无法触摸它:

App.SpinAdapter = DS.ActiveModelAdapter.extend({
  host: 'http://spin.example.com:8080',
  namespace: 'api/v1',

  init: function() {
    this._super();
    console.log('SpinAdapter...');
  }
});

App.SpinSerializer = DS.ActiveModelSerializer.extend({
  extract: function(store, type, payload, id, requestType) {
    console.log('SpinSezer#extract...' + type + '/' + id + '/' + requestType);
    return payload;
  },
});

App.PostAdapter = App.SpinAdapter.extend({});
App.PostSerializer = App.SpinSerializer.extend({});

App.CommentAdapter = App.SpinAdapter.extend({});
App.CommentSerializer = App.SpinSerializer.extend({});
4

1 回答 1

0

我找到了答案,而我在编辑问题后想得更多!

问题是被覆盖的extract方法。(关于我的上一个问题的详细信息,我可以在 Ember 数据上使用普通(rails 默认)JSON 响应吗?

当我覆盖 whole 时extract,作为问题的答案,我发现某些方法没有被调用。(也许做更多其他事情。)所以我只是用最小覆盖和调用_super.extract每个模型进行了测试。它有效!extractsuper

改变extract的方法是:

App.SpinAdapter = DS.ActiveModelAdapter.extend({
  host: 'http://spin.example.com:8080',
  namespace: 'api/v1',

  init: function() {
    this._super();
    console.log('SpinAdapter...');
  }
});

App.SpinSerializer = DS.ActiveModelSerializer.extend({
  extract: function(store, type, payload, id, requestType) {
    console.log('SpinSezer#extract...' + type + '/' + id + '/' + requestType);
    return this._super(store, type, payload, id, requestType);
  },
});

App.PostAdapter = App.SpinAdapter.extend({});
App.PostSerializer = App.SpinSerializer.extend({
  extract: function(store, type, payload, id, requestType) {
    return this._super(store, type, {posts: payload}, id, requestType);
  },
});

App.CommentAdapter = App.SpinAdapter.extend({});
App.CommentSerializer = App.SpinSerializer.extend({
  extract: function(store, type, payload, id, requestType) {
    return this._super(store, type, {comments: payload}, id, requestType);
  },
});

问题解决了。但可悲的是,由于posts没有关于的响应comments,(数据库和API结构是“只有孩子知道他们的父母”)如果我先加载帖子,则不会加载相关评论。一半的解决方案。

我只是尝试使用嵌套路由并希望自动加载嵌套 URL,但它没有发生。:-(

于 2015-03-24T02:09:49.123 回答