Code Monkey home page Code Monkey logo

Comments (20)

mgonto avatar mgonto commented on August 19, 2024

Hey,

I don't know 100% what you mean as you could use resolve in routeProvider for several things.

But, you can inject Restangular service through a resolve by simply stating the name.

You could also assign some Restangular fetch to the resolve. The resolve accepts either a String (name of the service) or a function that returns a value or a promise. Restangular's calls return a Promise, so you can return something like Restangular.all("accounts").getList() and routeProvider will ressolve it and give the return of that as a parameter if you need, but I think this is certainly NOT the best thing to do. If you need to inject a service, I'd use also the Controller function's params instead of the resolve.

Documentation is: http://docs.angularjs.org/api/ng.$routeProvider

I don't know if this solves your question. Please let me know.

Bests

from restangular.

skytracer avatar skytracer commented on August 19, 2024

Hi,

As far as I know, in the config part you can only inject providers and constants.
Why you do think is not a good idea to use Restangular.all("accounts").getList() in the resolve part?

Best

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Hey,

I haven't actually tried using this in the resolve.

Because if you make the requests in the resolve path and then use it in the controller (receive it as a parameter), I think it's more difficult to follow the code and understand errors. Suppose that your promise is rejected and you receive actually the error in there, you'd have to know how to handle it.

Instead, I think it's more cohesive to have Restangular service injected into the controller and then in the first line do the getList(). It's just an opinion.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Hey,

Just as a follow up, it turns out you can actually inject a service in the resolve so you can do something like:

resolve: {
  accounts: function(Restangular) {
    return Restangular.all("accounts").getList()
  }

That will let you receive in your controller a list of accounts.

Taken from example:

https://github.com/johnlindquist/angular-resolve/blob/master/client/js/app.js

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

I'm having trouble getting this to work myself. In my $routeProvider I have (in Coffeescript):

resolve: {
  resolvedItems: (Restangular) ->
    return Restangular.all("items").getList()
}

Then, in my controller, I have:

App.controller 'ItemController', ['$scope', ($scope, resolvedItems) ->
    $scope.items = resolvedItems
    ...
]

The controller then proceeds to fill a table with the returned items. When the view loads, though, my table fills with empty rows.

When I change the code back to the way it was originally, with $scope.items = Restangular.all("items").getList() and with no resolve, everything works as expected, with no resolving. Any idea as to the problem?

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Hey,

If you add in the resolve a promise of an Array with values already created, does this have the same problem? I don't know coffescript that well, but i guess that (Restangular) -> is an anonymous function, right?

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Restangular returns a promise, and the resolve expects a promise, so there should be no problem.

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

Hm, I'm pretty new to the concept of promises, so I'm not sure how I'd write the promise of an Array. Is there a quick way to do that?

And yes, (Restangular) -> is an anonymous function, it becomes

function(Restangular) {
    ...
}

when the coffeescript gets parsed.

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

Hmm, what's also odd is that when I change the code to be

resolve: {
    resolvedItems: function(Restangular) {
      var items = Restangular.all("items").getList();
      console.log(items);
      return items;
    }
}

in the routeProvider, and

    $scope.items = resolvedItems;
    console.log(resolvedItems);

in the controller, I get different output from the two log statements. The first gives an object with call, get, push, and then methods, and restangularCollection: true. The second gives an object with a lot of methods, like all, copy, extendCollection, extendModel, etc.

Does this tell you anything? Thanks so much for your help!

from restangular.

mgonto avatar mgonto commented on August 19, 2024

That tells me that it seems to work :).

In the resolve, items is actually the promise (it has the then) method, and in your controller, it has all of the properties for the resolved array, so it seems to be ok.

To create an array manually do a $q.when([1,2,3]) for example

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

Ok, so upon some further investigation, I figured out that the rows in my table - generated with an ng-repeat for item in items - correspond to the number of properties for the resolved array (27). It was confusing earlier because I happened to have the same number of items in my database.

Why do you think it's iterating through the properties and not the items in the array? This might be more of an Angular issue than a Restangular one, I'm not sure. What's weird, though, is that this only happens when it goes through resolve. Setting $scope.items = Restangular.all("items").getList() directly in the controller works just fine, and ng-repeat works normally.

Thanks!

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Does your server return an array? If it returns an object, do you convert it to an array in a requestInterceptor ?

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

My server returns an array, I think. I'm using Rails for a RESTful backend, so querying /api/v1/items gives me

[{
    name: "thing 1",
    description: "a thing"
},{
    ...
}]

etc.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

I don't know then what's going on :S, but I don't think it's Restangular

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

Gotcha. I'll keep looking into it and report back if I figure it out :)

Thanks again for your help!

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Sorry I cannot help anymore :(. I can tell you to console.log the result after the resolve, but if you get an object there instead of an array it's 100% weird

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

Yeah... console.log(resolvedItems) gives an object with a bunch of attributes that are functions. In the controller, console.log(Restangular.all("items").getList()) returns what looks like the promise (it has a then method), the same as in my above comment when I had console.log(items) in the routeConfig.

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

Figured it out! It wasn't a Restangular issue after all :) I'll write what I changed in the Angular code just in case someone else ends up in the same predicament:

First, I had to change my controller constructor (which was using array notation, as per the Angular dev guide) so that the dependencies listed in the array were all included in the function arguments, and in the same order (not very DRY, if you ask me, but that's an issue with Angular). I think what was happening was that dependencies were being injected, but when the controller function was called, they were being loaded into the wrong names. My above example was a bit simplified, as I had a few other dependencies being injected, so the problem isn't immediately apparent. Here's a more complete example of the problematic code:

App.controller 'ItemController', ['$scope', 'otherDependency', ($scope, resolvedItems, otherDependency) ->
    $scope.items = resolvedItems
    ...
]

So when the controller called $scope.items = resolvedItems, it was actually getting otherDependency. Here's what it should be:

App.controller 'ItemController', ['$scope', 'resolvedItems', 'otherDependency', ($scope, resolvedItems, otherDependency) ->
    $scope.items = resolvedItems
    ...
]

Second, I realized that my controller code was being executed twice - first, when it was loaded in the $routeProvider with controller: 'ItemController', and second when it was loaded in the view with <div ng-controller='GearController'>. Credit goes to this Stack Overflow answer for getting me to that solution.

Anyways, it's all working now. Thanks for your help, and thanks for the awesomeness that is Restangular!

from restangular.

mgonto avatar mgonto commented on August 19, 2024

I'm glad it worked.

Regarding the annotation and repetition, if you use grunt (or without it), you can use ngmin, which annotates it for you before releasing https://github.com/btford/ngmin

from restangular.

jamesmaniscalco avatar jamesmaniscalco commented on August 19, 2024

Cool, I'll have to try that out.

from restangular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.