对 emberjs 使用其后端方法作为 PHP 进行数据传输感到沮丧


Frustration with emberjs data transfering with its backend method as PHP

我试图做这三件简单的事情,但我无法在一个几乎一个月的时间里弄清楚,一个词---沮丧!现在我怀疑 emberjs 是否真的值得......

我想:

  1. 仅使用.php文件从数据库中获取数据,数据将采用 json 格式

    注意:我这样做了,在我问的其他SO问题中,所以我不会重复。

  2. 现在,问题是,如何在没有余烬数据的"存储"中保存这些数据?

  3. 有没有关于php如何与emberjs连接的可用教程?我试图阅读它的 REST 适配器,但似乎它需要某种类型的 JSON API。如果我使用纯 PHP 代码作为后端,是否需要开发 JSON API?

如果有什么部分不清楚,请在评论中回复!多谢。

Chen,说实话,在没有余烬数据的情况下处理数据非常容易。我用过几次,其中一些与构建相当大的前端系统有关。主要原因是,余烬数据当时并不那么稳定,也没有激发太多信心。另一个原因是,正如您将看到的,它非常简单。

您所要做的就是创建一个类(拥有一个单例 ember 控制器也很好),它将从服务器获取数据并将数据发布到服务器。数据格式应为 JSON,以使所有过程更轻松、更清晰。您可以通过使用简单的 ajax 调用来实现这一点,

function retrieveData(callback){
jQuery.ajax({
            url: *yoururl*,
            timeout: ajaxTimeOut,
            success: function(data){
/* here you store this data somewhere
it could be helpful to have a callback that you call so that this class/controller
is not coupled with specific logic*/
                if(callback!=null){
                    callback(data);
                }
            }
        });
}

检索数据的函数可以在调用关联的路由时调用,也可以从该路由的控制器调用。传递给前一个类/控制器的回调函数会将检索到的数据设置为控制器的某些属性,或者它甚至可以是它的模型。简单的例子,http://emberjs.jsbin.com/AsOcAbU/1/edit

.js

App = Ember.Application.create();
App.Router.map(function() {
  // put your routes here
});
App.IndexRoute = Ember.Route.extend({
  /*model: function() {
    return ['red', 'yellow', 'blue'];
  },*/
  setupController:function(controller,model){
      this.controllerFor('myJSON').findJSONData(function(data){
        controller.set('model',data);
      });
  }
});
App.IndexController = Ember.ArrayController.extend({
});
App.MyJSONController = Ember.Controller.extend({
  findJSONData:function(callback){
    var data = ['red', 'yellow', 'blue'];/*this will come from the server with an ajax call i.e. $.ajax({...})*/
    if(callback){
      callback(data);
    }
  }
});

哈佛商学院

   <script type="text/x-handlebars">
        <h2>Welcome to Ember.js</h2>
        {{outlet}}
      </script>
      <script type="text/x-handlebars" data-template-name="index">
      {{#if model}}
        <ul>
        {{#each item in controller}}
          <li>{{item}}</li>
        {{/each}}
        </ul>
        {{else}}
        loading ....
        {{/if}}
      </script>

在完整应用程序的上下文中,您可能需要迭代 json 数据并从中创建 ember 对象。这也非常简单,多亏了余烬,

http://emberjs.jsbin.com/oWetaDuH/1/edit

/*let's say you retrieve color objects from php*/
 findJSONData:function(callback){
  setTimeout(function(){
   /*this will come from the server 
     with an ajax call i.e. $.ajax({...})*/
    var data = [{color:'red'}, {color:'yellow'}, {color:'blue'}];
    if(callback){
      callback(data);
    }
  },2000);//mimic ajax call
 }
/*and you need to use ember magic bindings, so you have your own objects to represent the domain model*/
App.MyColor = Ember.Object.extend({
  color:null
});
/*then the code for retrieving will become*/
setupController:function(controller,model){
  this.controllerFor('myJSON').findJSONData(function(data){
        var myColors=[];
        data.forEach(function(jsonColor){
          myColors.pushObject(App.MyColor.create(jsonColor));/*this is all you need to create the ember object !!*/
        });
        controller.set('model',myColors);
      });
  }

哈佛商学院

<script type="text/x-handlebars" data-template-name="index">
  {{#if model}}
    <ul>
    {{#each item in controller}}
      <li>{{item.color}}</li>
    {{/each}}
    </ul>
    {{else}}
    loading ....
    {{/if}}
  </script>

更新

如果需要暂停布线直到解析模型,则应按照指南中的说明使用promises

http://emberjs.com/guides/routing/asynchronous-routing/

在这种情况下,由于jQuery ajax函数返回一个 promise,因此前面的示例可能如下所示:

http://emberjs.jsbin.com/mogicira/1/edit

.js

App.IndexRoute = Ember.Route.extend({
  model:function(controller,model){
      return    this.controllerFor('myJSON').findJSONData(function(data){
        var myColors=[];
        data.forEach(function(jsonColor){
          myColors.pushObject(App.MyColor.create(jsonColor));
        });
     return myColors;
      });
  }
});
App.IndexController = Ember.ArrayController.extend({
});
App.MyJSONController = Ember.Controller.extend({
  findJSONData:function(callback){
    return $.ajax({url:""}).then(function(data){
          data = [{color:'red'}, {color:'green'}, {color:'blue'}];
    if(callback){
       return callback(data);
    }
    });
  }
});

如果你喜欢余烬和余烬数据,它应该是为了减少代码和代码维护之间的其他不那么重要的问题

如果你对php感到满意,请将你的php代码移动到现代的FrameWork,因为不要重新发明weel,以获得与前端类似的好处。

我正在开发一个带有Codeigniter http://ellislab.com/codeigniter 和codeigniter rest服务 https://github.com/philsturgeon/codeigniter-restserver 的余烬应用程序,但我不建议您使用这种组合

我的意见你应该尝试Laravel http://net.tutsplus.com/tutorials/php/laravel-4-a-start-at-a-restful-api/或直接去Rails/Rails-API