2

私はこれらの 3 つの簡単なことをしようとしましたが、世界でほぼ 1 か月で理解することはできません。今、emberjsが本当に価値があるかどうか疑問に思っています...

したい:

  1. .php ファイルだけでデータベースからデータを取得すると、データは json 形式になります

    注:私が尋ねた他のSOの質問でこれを行ったので、繰り返しません。

  2. さて、問題は、これらのデータを残り火データなしで「ストア」に保存するにはどうすればよいですか?

  3. php が emberjs と接続する方法に関する利用可能なチュートリアルはありますか? その REST アダプターについて読み込もうとしましたが、ある種の JSON API が必要なようです。純粋な PHP コードをバックエンドとして使用する場合、JSON API を開発する必要がありますか?

何か不明な点があれば、コメントで返信してください!どうもありがとう。

4

2 に答える 2

3

Chen さん、正直なところ、 ember-data なしでデータを処理するのは非常に簡単です。私はそれを数回使用しましたが、そのうちのいくつかは非常に大規模なフロントエンド システムの構築に関連していました。その主な理由は、ember-data が当時それほど安定しておらず、あまり自信が持てなかったからです。もう 1 つの理由は、ご覧のとおり、非常に単純であるということです。

サーバーからデータを取得し、サーバーにデータを送信するクラスを作成するだけです (シングルトンの 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);
    }
  }

});

hbs

   <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 オブジェクトを作成する必要があります。それも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);
      });
  }

hbs

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

    });
  }

});
于 2013-11-11T19:15:46.277 に答える
2

あなたが ember と ember-data が好きなら、それはそれほど重要ではない他の懸念の間のより少ないコードとコードのメンテナンスのためのものであるべきです

PHP に慣れている場合は、PHP コードを最新の FrameWork に移行して、再発明せずに、フロントエンドで行われたのと同様の利点を得ることができます。

私は Codeigniter http://ellislab.com/codeigniterと codeigniter レスト サービスhttps://github.com/philsturgeon/codeigniter-restserverを使用したエンバー アプリを開発し ていますが、この組み合わせはお勧めしません。

私の意見では、Laravel http://net.tutsplus.com/tutorials/php/laravel-4-a-start-at-a-restful-api/を試すか、Rails/Rails-API に直接アクセスしてください。

于 2013-11-12T11:25:57.657 に答える