2013-08-17 2 views
1

컬렉션으로 파싱되어야하는 중첩 된 특성이있는 새 모델 양식 json을 만들 때 약간의 문제가 있습니다. 여기에 내가 오류에 도착하는 방법을 보여줍니다 : (어떤 도움을 주시면 감사)컬렉션 대신 배열로 생성 된 중첩 컬렉션

1) 나는이

user = new App.Models.User({"email":"[email protected]","id":1,"user_cities_attributes":[{"city_id":431,"user_action":"to-visit","user_id":1}] } 

이 같은 사용자를 생성)

와 무슨 일이 있었는지 콘솔에서 확인 후, 컬렉션이 생성되지 않았습니다. :

cities = new App.Collections.CitiesCollection() 
cities.fetch() 

userCity = new App.Models.UserCity({ user: user, city: cities.first(), user_action: "to-visit" }) 

S

user.get('user_cities') 
> undefined # where is my collection?! :(
user.get('user_cities_attributes') 
> [Object] # mmm weird... 

내가 컬렉션에 새로운 모델을 추가 한 후하려고하면 ...

이제 모든 것이 잘 될 것 같다 ... 무슨 일 ????

user.get('user_cities') 
> Backbone.Collection {length: 20, models: Array[20], _byId: Object, model: function, user: User…} 
user.get('user_cities_attributes') 
undefined 

모형

class App.Models.User extends Backbone.RelationalModel 
    paramRoot: 'user' 
    urlRoot: 'users' 

    defaults: 
    name: null 

class App.Models.UserCity extends Backbone.RelationalModel 
    paramRoot: 'user_city' 
    urlRoot: 'user_cities' 

    defaults: 
    user_action: null 

    relations: [{ 
    type: Backbone.HasOne, 
    key: 'user', 
    relatedModel: 'App.Models.User', 
    collectionType: 'App.Collections.UsersCollection', 
    keySource: 'user_id', 
    includeInJSON: 'id', 
    reverseRelation: { 
     key: 'user_cities', 
     keySource: 'user_cities_attributes', 
     includeInJSON: 'id' 
    } 
    }, 
    { 
    type: Backbone.HasOne, 
    key: 'city', 
    relatedModel: 'App.Models.City', 
    collectionType: 'App.Collections.CitiesCollection', 
    includeInJSON: 'id', 
    keySource: 'city_id', 
    reverseRelation: { 
     key: 'users', 
     includeInJSON: 'id' 
    } 
    }] 

class App.Models.City extends Backbone.RelationalModel 
    paramRoot: 'city' 
    urlRoot: 'cities' 

    defaults: 
    name: null 

    relations: [{ 
    type: Backbone.HasMany, 
    key: 'places', 
    relatedModel: 'App.Models.Place', 
    collectionType: 'App.Collections.PlacesCollection', 
    keySource: 'places_attributes', 
    # parse: true, 
    includeInJSON: 'id', 
    autoFetch: true, 
    reverseRelation: { 
     key: 'city', 
     includeInJSON: 'id' 
    } 
     # 'relatedModel' is automatically set to 'Zoo'; the 'relationType' to 'HasOne'. 
    }] 
+1

을 'reverseRelation'에 대한 주석을보고 coffeescript : http : // backbone을 사용하십시오. relational.org/#relations-reverseRelation. 그런 다음 여기에 설명 된대로'.setup'을 사용하십시오 : http://backbonerelational.org/#RelationalModel-setup – kalley

+0

그게 실제로 내 문제를 해결했습니다. 대답으로 게시하여 받아 들일 수 있습니까? 감사! – tomasbarrios

답변

1

reverseRelation에 대한 메모를보고 (에서 : http://backbonerelational.org/#relations-reverseRelation) : 커피 스크립트 사용하여 여기에 설명 된대로 .SETUP 사용하려고 그런

Note that if you define a relation (plus a reverseRelation) on a model, but don't actually create an instance of that model, it is possible initializeRelations will never get called, and the reverseRelation will not be initialized. This can happen when extend has been overridden, or redefined as in CoffeeScript. See setup.

을 : http://backbonerelational.org/#RelationalModel-setup