Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用其后端方法(如PHP)传输emberjs数据的挫折感_Php_Jquery_Json_Ember.js_Ember Data - Fatal编程技术网

使用其后端方法(如PHP)传输emberjs数据的挫折感

使用其后端方法(如PHP)传输emberjs数据的挫折感,php,jquery,json,ember.js,ember-data,Php,Jquery,Json,Ember.js,Ember Data,我试着做这三件简单的事情,但我在一个世界里几乎一个月都想不出来,一个词——挫折!现在我怀疑emberjs是否真的值得 我想: 只需一个.php文件即可从数据库中获取数据,数据将采用json格式 注意:我这样做了,所以我不再重复 现在,问题是,如果没有余烬数据,如何将这些数据保存在“存储”中 有关于php如何与emberjs连接的教程吗?我试图了解它的REST适配器,但它似乎需要某种类型的JSON API。如果我使用纯PHP代码作为后端,是否需要开发JSON API 如果有什么地方不清楚,请在评论

我试着做这三件简单的事情,但我在一个世界里几乎一个月都想不出来,一个词——挫折!现在我怀疑emberjs是否真的值得

我想:

  • 只需一个.php文件即可从数据库中获取数据,数据将采用json格式

    注意:我这样做了,所以我不再重复

  • 现在,问题是,如果没有余烬数据,如何将这些数据保存在“存储”中

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


  • 如果有什么地方不清楚,请在评论中回复!非常感谢。

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

    您所要做的就是创建一个类(拥有singleton 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);
                    }
                }
            });
    }
    
    检索数据的函数可以在调用关联路由时或从该路由的控制器调用。传递给前一个类/控制器的回调函数将把检索到的数据设置为控制器的某些属性,甚至可能是其模型。举个简单的例子,

    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>
    
    <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>
    
    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);
        }
    
        });
      }
    
    });
    
    hbs

    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>
    
    <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>
    
    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);
        }
    
        });
      }
    
    });
    
    和哈佛商学院

    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>
    
    <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>
    
    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代码移动到一个现代框架中,以避免重新发明weel,从而获得与ember对前端所做的类似的好处

    我正在开发一个包含Codeigniter和Codeigniter rest服务的ember应用程序,但我不建议您使用这种组合


    我认为你应该试试Laravel或者直接去Rails/Rails API

    @Mike B我做到了。但是,我从PHP获得的数据并没有真正存储在“存储”中,到目前为止,所有的方法都是使用余烬数据,所以我非常困惑……听起来你好像对一些事情感到困惑。在开始跳转到Ember/Ember数据之前,先用PHP创建restjsonapi可能是有益的。大多数ember后端都使用Rails/RailsAPI,但我相信,如果您擅长使用PHP编写RESTAPI,您可以找到一个简单的教程。祝你好运@Gowie47谢谢你的建议,我对emberjs描述的许多术语感到非常困惑。如果有jQuery(1年)和php(1年)的经验,通常需要多长时间才能理解emebr/emberdata?感谢您的分析!你认为进入Rails是一个很好的学习过程吗?因为我一直在读一篇关于它在启动时的困难和比PHP更大的学习曲线的文章。您认为了解入门级PHP的人能够管理Rail多久?谢谢!我不这么认为,我认为学习rails就像学习symphony或zend或ember。。。阅读代码示例和文档,学习惯例和编码、编码、编码,还可以考虑PHP REST调用的SLIM框架()。它很轻。哇。。。这似乎是一个非常详细的答案,首先非常感谢!顺便说一句,我一直很难理解findJSONData中“callback”变量的用途。它有什么用途?为什么需要使用它来调用数据?在本例中,“callback”是一个变量,是一个将数据转换为html代码的函数,因为您要检查它在函数中是否存在或有效?我对jQuery还不太熟悉,只是说:)谢谢@陈:是的,就是这样。回调是一个函数,来自ajax响应的数据被传递到该函数。您可以直接编写与数据相关的代码,即开始创建颜色对象,然而,通过这种方式,您可以将进行通信/ajax的代码与解析json到ember对象的代码解耦。@Chen将模拟ajax调用的
    settimeout
    更准确地移动到实际进行ajax调用的
    findJSONData
    函数。哼,所以我们基本上无法利用商店?因为我没有看到任何商店也被用在你的例子中。。。那太可悲了。。。事实上,我很期待这个商店的功能。这是否意味着如果我想使用商店,我必须编写API?@Chen商店和《emberjs指南》中模型部分描述的所有内容都与ember数据有关。因此,如果您不想使用余烬数据(我认为这是个问题),那么您基本上可以自己管理数据,正如我所示。正如你所看到的,这并不困难,但它总是取决于你想做什么。