Ember.Data JSON没有被推入商店

时间:2015-02-19 14:22:53

标签: json ember.js ember-data

我无法将我的API json结果推送到Ember Data对象模型数组中。我之前在我的路线上使用了getJSON,但是我现在需要能够过滤多个属性上的数据,而Ember Data提供了内置的方法......如果我可以得到它的工作。我对此很陌生,所以我可能会遗漏一些非常明显的东西。如果我遗漏了任何协助代码,请告诉我。当我查看promises时,它显示它们在从API中提取数据时已经完成但是当它尝试查询本地数据存储中的记录时显示为null(显示为null)我认为这意味着我的数据未被添加到商店在第一位。我会提供一个JSBin,但我们的api使用CORS,我无法获得JSbin来源进行身份验证。

在路线上我尝试了以下内容。两者都抛出不同的错误:

return this.store.find('restaurant');
//returns error: Error while processing route: casualdining  
//Array.prototype.map: 'this' is null or undefined.

return DineSection.Restaurant.find();
//Error while processing route: casualdining 
//Object doesn't support property or method 'find' 

申请代码:

DineSection = Ember.Application.create({
      rootElement: "#dinesection-app"  
  });

DineSection.Router.map(function () {         
      this.resource("casualdining");  
      this.resource("restaurant", { path: "/:id" });

  });

DineSection.ApplicationAdapter = DS.RESTAdapter.extend({
        host: 'https://api.lakecountyfl.gov',
        namespace: 'api/TourismListings',
        pathForType: function (type) {              
            return "GetRestaurants";   
        }
  });

DineSection.ApplicationStore = DS.Store.extend({
     adapter: 'DineSection.ApplicationAdapter'
  });

//SERIALIZE OUR JSON DATA FROM THE API into something ember data can use 
  DineSection.ApplicationSerializer = DS.RESTSerializer.extend({
      normalizePayload: function (type, payload) {
          return { restaurant: payload }
      }
  });

DineSection.Restaurant = DS.Model.extend({       
      name: DS.attr('string'),
      address: DS.attr('string'),
      city: DS.attr('string'),
      zip: DS.attr('string'),
      phone: DS.attr('string'),
      website: DS.attr('string'),
      facebook: DS.attr('string'),
      flickr: DS.attr('string'),
      hasphoto: DS.attr('boolean', {defaultValue: false}),
      outdoordining: DS.attr('boolean', { defaultValue: false }),
      breakfastprice: DS.attr('string'),
      lunchprice: DS.attr('string'),
      dinnerprice: DS.attr('string'),
      description: DS.attr('string'),
      primarycategoryname: DS.attr('string'),
      primarycategoryslug: DS.attr('string'),
      primarysubcategoryname: DS.attr('string'),
      primarysubcategoryslug: DS.attr('string'),         
      foodtype: DS.attr('string'),
      foodtypeid: DS.attr('number'),        
      ranking: DS.attr('number'),
      region1: DS.attr('boolean', { defaultValue: false }),
      region2: DS.attr('boolean', { defaultValue: false }),
      region3: DS.attr('boolean', { defaultValue: false }),
      region4: DS.attr('boolean', { defaultValue: false }) 
  });

DineSection.CasualdiningRoute = Ember.Route.extend({         
     model: function () {          
         //return Ember.$.getJSON("https://devapi.lakecountyfl.gov/api/TourismListings/GetRestaurants");               
         return this.store.find('restaurant');
         //return DineSection.Restaurant.find();
     }
 });

我的JSON响应看起来像这样(为简单起见,缩写为两个记录):

[{
"$id": "1",
"id": 1212,
"name": "Al's Landing",
"address": "111 W. Ruby St.",
"city": "Tavares",
"zip": "32778",
"phone": "352-555-8585",
"website": "http://www.alslanding.com",
"facebook": "https://www.facebook.com/pages/ALS-landing/110275062350544",
"flickr": null,
"hasphoto": true,
"outdoordining": true,
"breakfastprice": "N/A",
"lunchprice": "$10-$20",
"dinnerprice": "$10-$20",
"description": "A casual dining atmosphere with an indoor/ outdoor bar and plenty of outdoor lakefront seating.",
"primarycategoryname": "Dining",
"primarycategoryslug": "dining",
"primarysubcategoryname": "Casual Dining",
"primarysubcategoryslug": "casualdining",
"foodtype": "American",
"foodtypeid": 13,
"ranking": 3,
"region1": false,
"region2": false,
"region3": true,
"region4": false  
},
{
"$id": "2",
"id": 1026,
"name": "#1 Wok",
"address": "1080 E. Highway 50",
"city": "Clermont",
"zip": "34711",
"phone": "352-555-2346",
"website": "",
"facebook": "",
"flickr": "",
"hasphoto": false,
"outdoordining": false,
"breakfastprice": "N/A",
"lunchprice": "Less than $10",
"dinnerprice": "Less than $10",
"description": "",
"primarycategoryname": "Dining",
"primarycategoryslug": "dining",
"primarysubcategoryname": "Casual Dining",
"primarysubcategoryslug": "casualdining",
"foodtype": "Asian",
"foodtypeid": 1,
"ranking": 1,
"region1": false,
"region2": false,
"region3": false,
"region4": true
}
]

1 个答案:

答案 0 :(得分:0)

我认为您需要在JSON中使用“root”键,如下所示:   [餐馆:{