簡體   English   中英

使用json的ember-data hasMany關系

[英]ember-data hasMany relationship using json

我正在嘗試使用ember-data hasMany關系,以便我的作者鏈接可以復制所有作者,並且當您單擊特定作者時,該頁面還提供了指向他的書的鏈接。 單擊特定書籍時,它會提供指向其作者的鏈接。 它與fixtures完美配合,但是ajax調用出了點問題。 非常感謝您的幫助。

我的app.js:

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


App.Store = DS.Store.extend({
  adapter: DS.RESTAdapter.extend()
});

App.Author = DS.Model.extend({
  name: DS.attr('string'),
  biography: DS.attr('string'),
  book_ids: DS.hasMany('book', { async: true })
});

App.Book = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  image: DS.attr('string'),
  author_ids: DS.hasMany('author', { async: true })
});




App.Router.map(function() {
  this.resource('about');
  this.resource('books', function() {
    this.resource('book', { path: '/:book_id' });
  });
  this.resource('authors', function() {
    this.resource('author', { path: '/:author_id' });
  });
});

App.BooksRoute = Ember.Route.extend({
  model: function() {
  return App.Book.findAll();
 }
});

App.Book = Ember.Object.extend();

App.Book.reopenClass({
  findAll: function() {
  return $.getJSON('/json/books.json').then(function(response) {

    var books=[];

    response.book.forEach( function (book) {
      books.push( App.Book.create(book) );
    });
     return books; 
  });
 }
});

App.AuthorsRoute = Ember.Route.extend({
   model: function() {
   return App.Author.findAll();
 }
 });

App.Author = Ember.Object.extend();

App.Author.reopenClass({
  findAll: function() {
  return $.getJSON('/json/authors.json').then(function(response) {

            var authors=[];

      response.author.forEach( function (author) {
      authors.push( App.Author.create(author) );
    });
     return authors;
  });
 }
});

json中的數據表示如下:

{
  "author": [
  {
    "id": 1,
    "name": "James Joyce",
    "biography": "<p><b>James Joyce</b> <small>(February 2, 1882 - January 13, 1941)</small> was one of the most preeminent Irish authors of the twentieth century. He is known for his literary innovation such as a strictly focused narrative and indirect style. Although not strictly originally, James Joyce brought the aforementioned writing methods were to an unparalleled height.</p>",    
    "book_ids": ["1", "2"]
  },
  {
    "id": 2,
    "name": "F. Scott Fitzgerald",
    "biography": "<p><b>Francis Scott Key Fitzgerald</b> <small>(September 24, 1896 – December 21, 1940)</small> was an American author of novels and short stories, whose works are the paradigmatic writings of the Jazz Age, a term he coined. He is widely regarded as one of the greatest American writers of the 20th century. Fitzgerald is considered a member of the 'Lost Generation' of the 1920s. He finished four novels: This Side of Paradise, The Beautiful and Damned, The Great Gatsby (his most famous), and Tender Is the Night. A fifth, unfinished novel, The Love of the Last Tycoon, was published posthumously. Fitzgerald also wrote many short stories that treat themes of youth and promise along with age and despair.</p>",
    "book_ids": ["3", "4"]
   }
  ]
}

您正在編寫模型定義,然后不使用商店來獲取記錄。 我在jsbin中簡化了您的代碼。

App.BooksRoute = Ember.Route.extend({
  model: function() {
   return this.store.find('book');
 }
});


App.AuthorsRoute = Ember.Route.extend({
   model: function() {
     return this.store.find('author');
   }
 });

App.ApplicationAdapter= DS.RESTAdapter.extend({
  host:'json',
  pathForType: function(type) {
    return Ember.String.pluralize(type) + '.json';
  }
});

http://emberjs.jsbin.com/OxIDiVU/72/edit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM