Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Support data retrieval when using ngResource promises that return a object instead of a function. #18

Closed
wants to merge 1 commit into from

Conversation

lius
Copy link
Contributor

@lius lius commented Jul 8, 2014

No description provided.

l-lin added a commit that referenced this pull request Jul 8, 2014
l-lin added a commit that referenced this pull request Jul 8, 2014
@l-lin
Copy link
Owner

l-lin commented Jul 8, 2014

Sweet! Thanks!

@l-lin l-lin closed this Jul 8, 2014
@lius
Copy link
Contributor Author

lius commented Jul 8, 2014

Glad to contribute!

@l-lin
Copy link
Owner

l-lin commented Jul 9, 2014

However, for you information, with a promise, it's not possible to reload the data (ie send a request to the server again, and not just re-render the table).
Indeed, the promise, once resolved, does not re-send request to the server.
That's why the option handles a function that returns a promise so that the promise inside can be resolve multiple times.

@lius
Copy link
Contributor Author

lius commented Jul 9, 2014

Considering your advice and how ngResource works in recent versions, I think it's not possible pass a function that returns a promise directly from $resource calls, because actually the return is an object that holds the promise as one of it's properties, and not the promise (offering the possibility to bind directly to $scope without callbacks). This is what I was thinking when trying to change the code to accept a object too, since the use of $resource is a fairly common scenario in angular applications. What you think?

@l-lin
Copy link
Owner

l-lin commented Jul 9, 2014

Indeed, I always used $http or just $resource(...).$promise or a third party lib to fetch the data from the server. Didn't notice that angular evolved ngResource in a way we no longer need a callback.

I'm interested on how you manage to make the module works with the "convential" way.
My attempt did not succeed:

var Persons = $resource('data.json');
$scope.dtOptions = DTOptionsBuilder.fromFnPromise(Persons.query())
      .withPaginationType('full_numbers');

@lius
Copy link
Contributor Author

lius commented Jul 9, 2014

Using your example, I would change it to pass the promise has a parameter:

var Persons = $resource('data.json');
$scope.dtOptions = DTOptionsBuilder.fromFnPromise(Persons.query().$promise)
      .withPaginationType('full_numbers');

@lius
Copy link
Contributor Author

lius commented Jul 9, 2014

By the way, about binding using $resource without a callback (in recent versions):

$scope.people = Person.query();

@l-lin
Copy link
Owner

l-lin commented Jul 9, 2014

In that case, you can always wrap the promise with a function:

var Persons = $resource('data.json');
$scope.dtOptions = DTOptionsBuilder.fromFnPromise(function(){
        return Persons.query().$promise;
    })
    .withPaginationType('full_numbers');

@lius
Copy link
Contributor Author

lius commented Jul 9, 2014

Ok, It works. So, in this case, I think accepting objects alongside functions it's a matter of API decision to the library. It's up to you! Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants