Domanda

I have a single JSON file whose structure is not uniform to load into a single model. It has the data for a screen. The typical JSON structure is like below.

{
   "Model1":{
      "key1":"value1",
      "key2" : "value2"
   },
   "Model2":[
      {
        "key1":"value1",
        "key2" : "value2"
      },
      {
         "key1":"value1",
         "key2" : "value2"
         "subModel":[
            {
           "key1":"value1",
           "key2" : "value2"
            },
            {
                       "key1":"value1",
                "key2" : "value2"
            }
         ]
      }
   ]

Now I have to divide this JSON and load it into different models. From the server I will get only one JSON. How can I achieve it in SproutCore?

Research that I have done:

I have searched in Google with the phrase "How to load single JSON into SproutCore model?". However, I didn't get any results which answer my question. I have also searched on Stack Overflow. But I didn't get any results here either. Hence I didn't get any approach/ideas/inputs/approaches to try with, I don't have any code sample to show what I have tried.

È stato utile?

Soluzione

Assumptions:

  1. Your json is string.
  2. You have an SC.Store
  3. Your models are defined as SC.Record instances (e.g. App.Model1)

then you can do

var json = JSON.parse(yourJsonString);
var model1 = json["Model1"];
var model2Arr = json['Model2'];

//  loadRecord for a single instance
store.loadRecord(App.Model1, model1, model1.serverIdProp);
// loadRecords for a bunch of instances
store.loadRecords(App.Model2, model2Arr, model2Arr.getEach('serverIdProp'));

Note: your json should have some sort of id that the server assigns to your model instances.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top