javascript - In an ember model, how can you reference dynamic object keys? -


in our application passing array of objects server side model, , each element in array has key in it. instance...

[   {name: "dog", sound: "bark", food: "cats"},    {name: "cat", sound: "meow", food: "mouse"} ] 

in model defined so...

animals: hasmany(animal, {key: 'name', embedded: true }) 

then if want data cat, use findby feature find 1 name = "cat" so...

var animalname = "cat"; model.get('animals').findby('name', animalname); 

this works well, there lots of potential types of 'animal' objects, , know we're looking for.

however i'm curious other reasons if can pass in map server, becomes json object on client looks this...

animals : { "dog" : {sound: "bark", food: "cat"},             "cat" : {sound: "meow", food: "mouse"}           } 

it seems in order this, in model code need define "has a" relationship each potential animal type, dynamic , not want hard code options here. there way animals hasmany animals, there in map name, rather array?

i never figured out how inheritance way wanted, ended defining animals basic attribute...

animals: attr(), 

then putting object in , accessing data

animals.dog.sound 

or

var name = "dog"; animals[name].sound 

and works, although lose benefits of model inheritance system. application inheritance wasn't important.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -