Ember-data:“没有找到模型”,而它就在那里

时间:2013-12-31 11:56:08

标签: javascript php rest ember.js ember-data

我正在尝试从RESTful后端获取模型数据。这适用于模型“项目”,而对于模型“运河”,我只在控制台中收到一条错误消息:

  

断言失败:加载路径时出错:错误:未找到任何模型   为'0'

使用curl测试API工作正常。

router.js:

App.Router.map(function() {
    this.route("start", { path: "/" });
    this.route("projects", { path: "/projects" });
    this.route("canals", { path: "/canals" });
});


App.ProjectsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('project');
    }
});

App.CanalsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('canal');
    }
});

models.js

App.Project = DS.Model.extend({
    title: DS.attr('string'),
    client: DS.attr('string'),
    comment: DS.attr('string'),
    project_number: DS.attr('string')
});

App.Canal = DS.Model.extend({
    pid:  DS.attr('number'),
    street: DS.attr('string'), 
    district: DS.attr('string'), 
    upper_inspection_point: DS.attr('string'), 
    lower_inspection_point: DS.attr('string'), 
    type: DS.attr('string'), 
    direction: DS.attr('string'), 
    profile: DS.attr('string'), 
    height: DS.attr('string'), 
    width: DS.attr('string'), 
    material: DS.attr('string'), 
    branch: DS.attr('number'), 
    length: DS.attr('number'),
    demolition_class: DS.attr('number'),
    date: DS.attr('date'),
    comment: DS.attr('string')
});

app.js

window.App = Ember.Application.create();
App.Store = DS.Store.extend({
    adapter: DS.RESTAdapter,
});
DS.RESTAdapter.reopen({
  namespace: 'api/index.php'
});

依赖关系/版本

DEBUG: ------------------------------- ember-1.2.0.js:3231
DEBUG: Ember      : 1.2.0 ember-1.2.0.js:3231
DEBUG: Ember Data : 1.0.0-beta.5+canary.e1200068 ember-1.2.0.js:3231
DEBUG: Handlebars : 1.1.2 ember-1.2.0.js:3231
DEBUG: jQuery     : 2.0.3 ember-1.2.0.js:3231
DEBUG: ------------------------------- ember-1.2.0.js:3231
Ember Debugger Active 

修改 看来,后端(SlimPHP)提供了不兼容的JSON响应(http://jsfiddle.net/hk2ph/)。我将fetchAll()更新为fetchAll(PDO :: FETCH_ASSOC),但我仍然得到编号索引:

$sql = "SELECT * FROM canals WHERE pid = :pid";
        try {
            $db = getConnection();
            $stmt = $db->prepare($sql);
            $stmt->bindParam(":pid", $pid);
            $stmt->execute();
            $canals = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $db = null;
            header("Content-Type: application/json");
            echo json_encode($canals);
            exit;
        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
            header("Content-Type: application/json");
            exit;
        }

3 个答案:

答案 0 :(得分:4)

这是你的json,它正在返回一个属性,使得ember数据认为有0类型,有点像这样:

{
  canals:[
    {
      // stuff
    }],
  0: [
   {

   }]
}

答案 1 :(得分:1)

构建JSON的正确方式是:

$canals = $stmt->fetchAll(PDO::FETCH_ASSOC);

现在一切正常,感谢您的支持!

答案 2 :(得分:1)

检查您的JSON api响应是否按照ember-data的方式进行结构化。您应该提供所有这样的资源数据:

{
  "items": [
    {
      "id": 0,
      "title": "foo"
    },
    {
      "id": 1,
      "title": "bar"
    }
  ]
}

来源: Ember 1.0.0 RESTAdapter failure