简体   繁体   English

将PHP用作Ember.js的后端

[英]Using PHP as a Backend with Ember.js

I am trying to use Emberjs with PHP as backend. 我正在尝试将Emberjs与PHP一起用作后端。

Here's my app.js , 这是我的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代码,

<?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? 我可以看到它正在将.json添加到php文件中,但是为什么呢? Moreover, how do I get around with it or how do I implement my own Ajax calls in Ember? 此外,如何解决它或如何在Ember中实现自己的Ajax调用? Moreover, I am using Ember Model in the code. 此外,我在代码中使用Ember Model。

ember-model does not provide out pf the box any configuration option to change this behaviour, eg adding the .json at the end of the URL. ember-model没有在盒子外面提供任何配置选项来更改此行为,例如,在URL的末尾添加.json

So a possible solution could be to reopen the RESTAdapter and override the buildURL function to not include the .json . 因此,一个可能的解决办法是重新打开RESTAdapter并覆盖buildURL功能,不包括.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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM