在EmberJS中进行AJAX GET和POST调用的正确方法 [英] Right way to make AJAX GET and POST calls in EmberJS

查看:174
本文介绍了在EmberJS中进行AJAX GET和POST调用的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用EmberJS,我绝对喜欢它。它确实有一个学习曲线,但我相信它有根本的非常有意义的原则。



我的问题是如何在Ember JS中进行GET和POST调用。我知道有模型/商店,但模型(在我看来)将只是一个特定的实体的属性。



对以下问题的任何想法都会很棒。

  1 。用户发送朋友请求给用户B.应该有一个请求
模型?我只是做一个POST请求?

2.需要为页面返回一些任意数据。不是
特别是一个模型。我还应该为此做一个模型吗?
使用简单的GET请求?

3.用户需要更新此配置文件照片。如何将文件
上传,设置为模型属性?

我应该如何进行常规的GET和POST调用(如果我要完成这些) 。只需使用jQuery的$ .ajax()或是另一种方式。我还发现一个服务 ember-ajax ,将$ .ajax扩展为承诺风格。



任何想法都将不胜感激。



长寿EmberJS:)

解决方案

第一个选项:你可以使用 ember-data 。它具有自定义设置,例如序列化程序适配器



第二个选项您可以使用 ember-ajax 等附件。



我们的使用只是使用jQuery的 ajax()。我们写了一个只包含 jquery.ajax()的服务,并在我们的代码中随处可见。我们相信它给我们写了不同类型的查询的灵活性。我们没有任何型号 ember-data



示例 - - 代码:

 导出默认值Ember.Service.extend({
doPostCall(target,data,options = null){
//考虑使用Ember的克隆选项$。extend
var requestOptions = options || {};
requestOptions.url = target;
requestOptions.type ='POST';
requestOptions.data = JSON.stringify(data);
doRemoteCall(requestOptions);
},

doGetCall(target,data = null,options = null){
//考虑使用Ember的克隆选项$。extend
var requestOptions = options || {};
requestOptions.url = target;
requestOptions.type ='GET';
requestOptions.data = data;
doRemoteCall(requestOptions);
},

doRemoteCall requestOptions){
//在此处指定默认值:
//如contentType,dataType ,withCredentials ...

Ember。$。ajax(requestOptions)
.then(function(data){
Ember.run(null,resolve,data);
},function(jqXHR,textStatus,errorThrown){
jqXHR.then = null;
Ember.run(null,reject,jqXHR,textStatus,errorThrown);
});
}
});

PS:顺便说一句,如果您需要在服务器端运行应用程序(使用fastboot) ,你不能使用jQuery。所以使用 ember-network


I've started working on EmberJS and I absolutely love it. It does have a learning curve but I believe it has fundamentally very meaningful principles.

My questions is how to make GET and POST calls in Ember JS. I understand that there are models / store, but models (in my opinion) would be to only a particular entity's attributes.

Any thoughts on the following questions would be great.

1. USER A send friend request to USER B. Should there be a "Request"
   model? And do I just make a POST request?

2. Some arbitrary data needs to be returned for the page. Not
   particularly of a model. Should I still make a model for that?
   For use a simple GET request?

3. User needs to update this profile photo. How can the file
   to be uploaded, be set as a model attribute?

How should I go about making regular GET and POST calls (if I am to do them at all). Just use jQuery's $.ajax() or is there another way. I also found a service ember-ajax which has extended the $.ajax into a promises style.

Any thoughts would be much appreciated.

Long live EmberJS :)

解决方案

First option: You can use ember-data. It has customizations such as serializers or adapters.

Second option: You can use addons like ember-ajax.

Our usage is just using jQuery's ajax(). We wrote a service that just wraps jquery.ajax() and use it everywhere in our code. We believe that it gives us a flexibility of writing different kind of queries. We don't have any model of ember-data.

Sample -pseudo- code:

  export default Ember.Service.extend({
    doPostCall(target, data, options=null){
       //consider cloning options with Ember.$.extend
       var requestOptions= options || {};
       requestOptions.url=target;
       requestOptions.type='POST';
       requestOptions.data=JSON.stringify(data);
       doRemoteCall(requestOptions);
    },

    doGetCall(target, data=null, options=null){
       //consider cloning options with Ember.$.extend
       var requestOptions=options || {};
       requestOptions.url=target;
       requestOptions.type='GET';
       requestOptions.data=data;
       doRemoteCall(requestOptions);
    },

    doRemoteCall(requestOptions){
      //assign default values in here:
      //  such as contentType, dataType, withCredentials...

      Ember.$.ajax(requestOptions)
        .then(function(data) {
            Ember.run(null, resolve, data);
         }, function(jqXHR , textStatus, errorThrown) {
            jqXHR.then = null;
            Ember.run(null, reject, jqXHR, textStatus, errorThrown);
         });
    }
  });

PS: By the way, if you need to run your app in server-side (with fastboot), you cannot use jQuery. So use ember-network.

这篇关于在EmberJS中进行AJAX GET和POST调用的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆