douzong5057 2013-08-27 19:48
浏览 50
已采纳

使用PHP作为Ember.js的后端

I am trying to use Emberjs with PHP as backend.

Here's my app.js,

App = Ember.Application.create({});

App.IndexRoute = Ember.Route.extend({
    renderTemplate : function(controller) {
        this.render('MyApp', {
            controller : controller
        });
    },
    model : function() {
        return App.MyTemplateModel.find();
    }
});

App.IndexController = Ember.ArrayController.extend({

    filteredContent : Ember.computed.oneWay("content"),
    last : function() {
        var lastName = App.controller.get('selectedProgrammer.last_name');
        var filtered = this.get('content').filterProperty('last_name', lastName);
        this.set("filteredContent", filtered);

    },
    refresh : function() {
        var refresh = this.get('content');
        this.set("filteredContent", refresh);
    }
});

App.MyTemplateModel = Ember.Model.extend({
    id : Ember.attr(),
    last_name : Ember.attr(),
    first_name : Ember.attr(),
    suffix : Ember.attr(),
    expiration : Ember.attr()
});

App.controller = Ember.Object.create({
    selectedProgrammer : null,
    content : [Ember.Object.create({
        last_name : "Solow",
        id : 1
    }), Ember.Object.create({
        last_name : "Arbogast",
        id : 2
    }), Ember.Object.create({
        last_name : "Dorfman",
        id : 3
    }), Ember.Object.create({
        last_name : "Eliason",
        id : 4
    })]
});

App.MyTemplateModel.url = "user.php";
App.MyTemplateModel.adapter = Ember.RESTAdapter.create({
  ajaxSettings: function(url, get) {
    return {
      url: url,
      type: get
    };
  }

});
var existing = App.MyTemplateModel.find();
App.MyTemplateModel.camelizeKeys = true;

and my PHP code,

<?php

$con = mysql_connect("localhost","root","school");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("codeigniter", $con);
$id = $_GET['id'];

$query = "SELECT * FROM `user`";

$comments = mysql_query($query);

while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
   $name = $row['first_name'];

   echo json_encode($name);
}

mysql_close($con);

?>

But I am getting this on console,

GET http://ember.local/user.php.json 404 (Not Found) 

I can see it is adding .json to php file but why? Moreover, how do I get around with it or how do I implement my own Ajax calls in Ember? Moreover, I am using Ember Model in the code.

  • 写回答

1条回答 默认 最新

  • duan0504 2013-08-27 20:17
    关注

    ember-model does not provide out pf the box any configuration option to change this behaviour, e.g. adding the .json at the end of the URL.

    So a possible solution could be to reopen the RESTAdapter and override the buildURL function to not include the .json.

    Ember.RESTAdapter.reopen({
      buildURL: function(klass, id) {
        var urlRoot = Ember.get(klass, 'url');
        if (!urlRoot) { throw new Error('Ember.RESTAdapter requires a `url` property to be specified'); }
    
        if (!Ember.isEmpty(id)) {
          return urlRoot + "/" + id;
        } else {
          return urlRoot;
        }
      }
    });
    

    But this is not that future proof if the original code changes and you want to update the lib you had to change also your override.

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献