0

これは私を夢中にさせています。簡単なデータ モデルを設定しました (Padrino を使用)。実際にエラー メッセージが表示される段階はとうに過ぎていますが、「App.Repo」モデルを「App.Stack」モデルに追加するだけでは機能しません。

App.Store = DS.Store.extend({
  revision: 10
  adapter: DS.RESTAdapter.create({
    bulkCommits: false,
    mappings: {
      stars: App.Stars,
      stacks: App.Stacks
    }
  })
});

App.Stack = DS.Model.extend({
  url: DS.attr('string'),
  repos: DS.hasMany('App.Repo')
});

App.Repo = DS.Model.extend({
  name: DS.attr('string'),
  url: DS.attr('string'),
  description: DS.attr('string'),
  language: DS.attr('string'),
  watchers: DS.attr('number'),
  stack: DS.belongsTo('App.Stack'),
  stackId: DS.attr('number')
});

var store = App.get('router.store');
newStack = store.createRecord(App.Stack);
console.log(newStack.serialize())
-> Object {url: null}    // no mention of a repos array like I was expecting?
newStack.set('url', 'http://google.com');
console.log(newStack.serialize());
-> Object {url: "http://google.com"}    // this works though


var repo = App.Repo.find().objectAt(0);
console.log(repo.serialize());
-> Object {name: "floere/james", url: "https://github.com/floere/james", description: "Voice commanded servant for OSX", language: "Ruby", watchers: 97…}
// so this exists too…

repos = newStack.get('repos');
repos.pushObject(repo);
newStack.get('repos.length'); // 1 (repos.toArray() etc etc all work too)

// but then…
console.log(newStack.serialize())
-> Object {url: null}

// and so then I try to save the relationship on the server anyway…
store.commit()
=> {"stack"=>{"url"=>nil}} // in my Ruby server logos

ストアはすべて、バックエンドとうまく通信するように設定されています (たとえば、POST を /repo.json に送信すると、正しい要求が送信されます)。App.Stack に関係があることを認識していないだけです。

何が問題なのか、何を調べればよいのかわかりません:(

また

Ruby コンソールでリレーションを作成してから、ビューでそれらにアクセスしてみました。これが起こることです

// in the router
router.get('applicationController').connectOutlet('body', 'stacks', router.get('store').findAll(App.Stack));
// in the view
<script type="text/x-handlebars" data-template-name="stacks">
{{#each stack in controller }}
  {{stack.id}} // this works
  {{stack.url}} // this works
  {{stack.repos.length}} // this returns the correct count
  {{#each repo in stack.repos}}
    // this loops the right number of times. so there *is* something there. somehow.
    {{repo}} // prints out <App.Repo:ember490>
    {{repo.id}} // prints out [object Object]
  {{/each}}
{{/each}}

最後に、[object Object] にヒントがあるのではないでしょうか?

私はとても迷っています:(

より詳しい情報:

私は Mongoid で Padrino を使用しており、RABL を使用して JSON を提供しています。前述したように、Stack および Repo レコードを照会してテンプレート化できます。/stacks.json エンドポイントの JSON サンプルを次に示します。

{
"stacks": [
    {
        "account_id": null, 
        "id": "50c127ff6f094144ed000001", 
        "stars": [
            {
                "description": "Voice commanded servant for OSX", 
                "id": "50c128996f0941cfe8000001", 
                "name": "floere/james"
            }
        ]
    }
]
}
4

2 に答える 2

0

repos配列をループして、hasManyリレーションシップをjsonオブジェクトに手動で追加する必要があると思います。私はアダプターのcreateRecordメソッドでこれを行っています。

createRecord: (store, type, record) ->
  data = {}
  data[root] = @toData(record, { includeId: true })

  repos = []
  stack.get("repos").forEach (repo) ->
    repos.pushObject repo.serialize()

  data[root]["repos"] = repos

  ...
于 2012-12-07T01:37:43.220 に答える
0

JSON に埋め込まれた関連オブジェクトを適切にロードする方法を見つけました。基本的に、シリアライザーをサブクラス化し、そのイニシャライザーで関係のマップを登録するように指示する必要があります。以下は、対多関係「resourceTypes」を持つ Category というモデル クラスの例です。

App.WOSerializer = DS.Serializer.extend({
   init: function(){
     this._super();
     this.map(App.Category, {
       resourceTypes: { embedded: 'load' }
     });
   }
});

私の解決策はさらにここで説明されています。

于 2012-12-07T14:42:45.340 に答える