Ember.js pre.4,RESTAdapter,有很多关系

Ember.js pre.4,RESTAdapter,有很多关系,ember.js,ember-data,ember-router,Ember.js,Ember Data,Ember Router,我正在尽我最大的努力在最新版本的ember.js和使用RESTAdapter的ember数据中找到和/或拼凑出一个hasMany/belongsTo关系的工作JSIDle。到目前为止,我已经找到了一个使新路由器经过一些探索的方法和一个利用必要的DS关系的方法,但是为了简洁起见,绕过了路由器 我摸索的WIP试图将这些内容拼凑成一个内聚的示例,可以在这里找到:。很明显,我是一个新加入ember.js的人,这把小提琴有很多错误,所以请原谅我缺乏技巧 App.Store = DS.Store.exten

我正在尽我最大的努力在最新版本的ember.js和使用RESTAdapter的ember数据中找到和/或拼凑出一个hasMany/belongsTo关系的工作JSIDle。到目前为止,我已经找到了一个使新路由器经过一些探索的方法和一个利用必要的DS关系的方法,但是为了简洁起见,绕过了路由器

我摸索的WIP试图将这些内容拼凑成一个内聚的示例,可以在这里找到:。很明显,我是一个新加入ember.js的人,这把小提琴有很多错误,所以请原谅我缺乏技巧

App.Store = DS.Store.extend({
  revision: 11,
  adapter: DS.RESTAdapter.create({})
});

App.Post = DS.Model.extend({
    title: DS.attr('string'),
    post: DS.attr('string'),
    comments: DS.hasMany('App.Comment')
});


App.Comment = DS.Model.extend({
    post: DS.belongsTo('App.Post'),
    description: DS.attr('string')
});

store = App.__container__.lookup('store:');

store.load(App.Post, {
    id: 1,
    title: 'Post 1 Title',
    post: 'Body of post 1',
     comments:[1,2]
    },
       {
    id: 2,
    title: 'Post 2 Title',
    post: 'text of post 2',
     comments:[3,4]
},
{
    id: 3,
    title: 'Post 3 title',
    post: 'text of post3',
     comments:[5,6]
}
      );
store.load(App.Comment, {id: 1, description: "Great post!"},
       App.Comment, {id: 2, description: "Post sucks."},
       App.Comment, {id: 3, description: "Nice style"},
       App.Comment, {id: 4, description: "Horrible writing"},
       App.Comment, {id: 5, description: "Ember.js FTW"},
       App.Comment, {id: 6, description: "Get up get out n' get something"}

      );
如果有人能为我指出正确的方向,让这把小提琴发挥作用,或者链接到pre.4与RestaAdapter和hasMany的工作示例,我将永远感谢你的慷慨


谢谢你

你的小提琴只有几个语法问题。我已在此处使用工作版本对其进行了更新:

1您没有正确加载商店。要在同一调用中加载多个项,需要使用loadMany,传入模型类和数组

因此,不是:

store.load(App.Post, {
  id: 1,
  title: 'Post 1 Title',
  post: 'Body of post 1',
   comments:[1,2]
},
{
  id: 2,
  title: 'Post 2 Title',
  post: 'text of post 2',
  comments:[3,4]
},
{
  id: 3,
  title: 'Post 3 title',
  post: 'text of post3',
   comments:[5,6]
});

store.load(App.Comment, {id: 1, description: "Great post!"},
  App.Comment, {id: 2, description: "Post sucks."},
  App.Comment, {id: 3, description: "Nice style"},
  App.Comment, {id: 4, description: "Horrible writing"},
  App.Comment, {id: 5, description: "Ember.js FTW"},
  App.Comment, {id: 6, description: "Get up get out n' get something"}
);
{{#each comment in post.comments}}
  {{comment.description}}
{{/each}}
应该是:

store.loadMany(App.Post, [
  { id: 1, title: 'Post 1 Title', post: 'Body of post 1', comments: [1,2] },
  { id: 2, title: 'Post 2 Title', post: 'text of post 2', comments: [3,4] },
  { id: 3, title: 'Post 3 title', post: 'text of post 3', comments: [5,6] }
]);

store.loadMany(App.Comment, [
  { id: 1, description: "Great post!" },
  { id: 2, description: "Post sucks." },
  { id: 3, description: "Nice style" },
  { id: 4, description: "Horrible writing" },
  { id: 5, description: "Ember.js FTW" },
  { id: 6, description: "Get up get out n' get something" }
]);
{{#each comment in content.comments}}
  {{comment.description}}
{{/each}}
2您对每个对象的把手模板调用引用了错误的属性

而不是:

store.load(App.Post, {
  id: 1,
  title: 'Post 1 Title',
  post: 'Body of post 1',
   comments:[1,2]
},
{
  id: 2,
  title: 'Post 2 Title',
  post: 'text of post 2',
  comments:[3,4]
},
{
  id: 3,
  title: 'Post 3 title',
  post: 'text of post3',
   comments:[5,6]
});

store.load(App.Comment, {id: 1, description: "Great post!"},
  App.Comment, {id: 2, description: "Post sucks."},
  App.Comment, {id: 3, description: "Nice style"},
  App.Comment, {id: 4, description: "Horrible writing"},
  App.Comment, {id: 5, description: "Ember.js FTW"},
  App.Comment, {id: 6, description: "Get up get out n' get something"}
);
{{#each comment in post.comments}}
  {{comment.description}}
{{/each}}
应该是:

store.loadMany(App.Post, [
  { id: 1, title: 'Post 1 Title', post: 'Body of post 1', comments: [1,2] },
  { id: 2, title: 'Post 2 Title', post: 'text of post 2', comments: [3,4] },
  { id: 3, title: 'Post 3 title', post: 'text of post 3', comments: [5,6] }
]);

store.loadMany(App.Comment, [
  { id: 1, description: "Great post!" },
  { id: 2, description: "Post sucks." },
  { id: 3, description: "Nice style" },
  { id: 4, description: "Horrible writing" },
  { id: 5, description: "Ember.js FTW" },
  { id: 6, description: "Get up get out n' get something" }
]);
{{#each comment in content.comments}}
  {{comment.description}}
{{/each}}
因为保存post数据的是content属性


干杯

你的小提琴只有几个语法问题。我已在此处使用工作版本对其进行了更新:

1您没有正确加载商店。要在同一调用中加载多个项,需要使用loadMany,传入模型类和数组

因此,不是:

store.load(App.Post, {
  id: 1,
  title: 'Post 1 Title',
  post: 'Body of post 1',
   comments:[1,2]
},
{
  id: 2,
  title: 'Post 2 Title',
  post: 'text of post 2',
  comments:[3,4]
},
{
  id: 3,
  title: 'Post 3 title',
  post: 'text of post3',
   comments:[5,6]
});

store.load(App.Comment, {id: 1, description: "Great post!"},
  App.Comment, {id: 2, description: "Post sucks."},
  App.Comment, {id: 3, description: "Nice style"},
  App.Comment, {id: 4, description: "Horrible writing"},
  App.Comment, {id: 5, description: "Ember.js FTW"},
  App.Comment, {id: 6, description: "Get up get out n' get something"}
);
{{#each comment in post.comments}}
  {{comment.description}}
{{/each}}
应该是:

store.loadMany(App.Post, [
  { id: 1, title: 'Post 1 Title', post: 'Body of post 1', comments: [1,2] },
  { id: 2, title: 'Post 2 Title', post: 'text of post 2', comments: [3,4] },
  { id: 3, title: 'Post 3 title', post: 'text of post 3', comments: [5,6] }
]);

store.loadMany(App.Comment, [
  { id: 1, description: "Great post!" },
  { id: 2, description: "Post sucks." },
  { id: 3, description: "Nice style" },
  { id: 4, description: "Horrible writing" },
  { id: 5, description: "Ember.js FTW" },
  { id: 6, description: "Get up get out n' get something" }
]);
{{#each comment in content.comments}}
  {{comment.description}}
{{/each}}
2您对每个对象的把手模板调用引用了错误的属性

而不是:

store.load(App.Post, {
  id: 1,
  title: 'Post 1 Title',
  post: 'Body of post 1',
   comments:[1,2]
},
{
  id: 2,
  title: 'Post 2 Title',
  post: 'text of post 2',
  comments:[3,4]
},
{
  id: 3,
  title: 'Post 3 title',
  post: 'text of post3',
   comments:[5,6]
});

store.load(App.Comment, {id: 1, description: "Great post!"},
  App.Comment, {id: 2, description: "Post sucks."},
  App.Comment, {id: 3, description: "Nice style"},
  App.Comment, {id: 4, description: "Horrible writing"},
  App.Comment, {id: 5, description: "Ember.js FTW"},
  App.Comment, {id: 6, description: "Get up get out n' get something"}
);
{{#each comment in post.comments}}
  {{comment.description}}
{{/each}}
应该是:

store.loadMany(App.Post, [
  { id: 1, title: 'Post 1 Title', post: 'Body of post 1', comments: [1,2] },
  { id: 2, title: 'Post 2 Title', post: 'text of post 2', comments: [3,4] },
  { id: 3, title: 'Post 3 title', post: 'text of post 3', comments: [5,6] }
]);

store.loadMany(App.Comment, [
  { id: 1, description: "Great post!" },
  { id: 2, description: "Post sucks." },
  { id: 3, description: "Nice style" },
  { id: 4, description: "Horrible writing" },
  { id: 5, description: "Ember.js FTW" },
  { id: 6, description: "Get up get out n' get something" }
]);
{{#each comment in content.comments}}
  {{comment.description}}
{{/each}}
因为保存post数据的是content属性


干杯

顺便说一句,@zgramana fiddle的链接是-我刚刚注册了stackoverflow,所以我最多只能发布两个链接,直到我达到10。干杯顺便说一句,@zgramana fiddle的链接是-我刚刚注册了stackoverflow,所以我最多只能发布两个链接,直到我达到10。干杯太棒了,非常感谢!我不能投票给你的答案,直到我得到15个代表,但我把它标记为已回答。作为旁注,您知道为什么单击一次后导航栏和下一个>>按钮链接到路由会中断吗?干杯太棒了,非常感谢!我不能投票给你的答案,直到我得到15个代表,但我把它标记为已回答。作为旁注,您知道为什么单击一次后导航栏和下一个>>按钮链接到路由会中断吗?干杯