question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[example request] Basic usage with `ember-ajax`

See original GitHub issue

There are example that uses low-level xhr, but the most common use case (apart from Ember Data) is ember-ajax.

What is the most efficient and compact way of using ember-concurrency with ember-ajax? Is it possible to cancel ember-ajax requests?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
robclancycommented, Feb 7, 2017

ember-ajax now supports this on master. Not sure if there are plans for ember-concurrency to cancel the requests itself when things like a component gets destroyed.

Here is an example of what I am doing (changed code from what I actually used so could be mistakes)

    let promise;
    try {
      promise = yield this.requestSomethingWithEmberAjax();
    } finally {
      promise.xhr.abort();
    }
1reaction
machtycommented, Feb 7, 2017

@robclancy that looks good to me; do you even need to check ready state? In my experience abort()ing a complete XHR just no-ops.

In an upcoming version of EC it’ll be possible to import a symbol from EC and use it to implement a method on objects or their prototypes so that when they’re yielded in an EC task, it’ll provide EC with extra information as to how to cancel them.

The goal is that you’d be able to implement this fn on ember-ajax’s promise, something like

https://github.com/ember-cli/ember-ajax/blob/master/addon/-private/promise.js

import AJAXPromise from 'ember-ajax/-private/promise';
import { YIELDABLE } from 'ember-concurrency'; // this API not on master yet

AJAXPromise.protoype[YIELDABLE] = function * (promise, taskInstance) {
    try {
      return yield this.requestSomethingWithEmberAjax();
    } finally {
      promise.xhr.abort();
    }
}

This would make it so that anywhere else in your code you could just write

  ajax: Ember.inject.service(),
  myTask: task(function * () {
    let a = yield this.get('ajax').request('/posts');
    let b = yield this.get('ajax').request('/posts');
    let c = yield this.get('ajax').request('/posts');
  })

I’m not 100% I’ll be exposing this since it’s kind of changes behavior everywhere and might “over-cancel” in some cases but I’m leaning towards adding it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Service for making AJAX requests in Ember applications
Basic Usage. The AJAX service provides methods to be used to make AJAX requests, similar to the way that you would use jQuery.ajax...
Read more >
how send Post request with ajax in ember.js? - Stack Overflow
1 Answer 1 · 2. It's better to use ember-ajax now. · Yes, ember ajax is a better solution: It is here: github.com/ember-cli/ember-ajax...
Read more >
Loading and Saving Data With jQuery's Ajax - Ember Guides
When your user enters a route, Ember will ask the associated Ember.Route object to convert the current URL into a model. For example,...
Read more >
ember-ajax - npm
Ajax Service. Basic Usage. The AJAX service provides methods to be used to make AJAX requests, similar to the way that you would...
Read more >
Ember Ajax and Ember Simple Auth | Timmy O'Mahony
Ember Ajax is centered around a service that you can use in your routes, controllers and components. This service has a request(.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found