Sander thanks for that, I know a little bit about resolve. Your saying I
can use resolve and have a function where I make a call to my DataFactory
before we go to the route so that I have my data once the view is loaded
right?
Just looked at your plunkr, a lot to take in lol I need to go back to it
and view it when I have more time.
I like the idea of using resolve. Can you use resolve as well as using a
custom controller variable to control member access?
for example, resolving first to grab the data, then also having something
like restrict: true
i.e.
when('/whatever', {
templateUrl: 'someurl',
controller: 'someController',
resolve: {
var data = DataFactory.getData().then(function() {
// do some action
})
},
restrict: true
})
pardon me for not using a plnkr, I am at work and often I just don't have
the time to do them. These projects I work on are personal projects, not
work projects so I can't do this during work.
Also THANK YOU for the named template idea. I like how you are using a
name: variable on each route to be able to use it for example with
highlighting the right button for which view you are on. I love that idea.
I m going to use it
I am just learning angular so although I know how resolve can work, I
haven't developed good habbits yet. This is why I ask so many "is this
good/ bad practice" type of questions. Just trying to learn how to do
things in angularjs coming from jquery and plain javascript
Marcos,
Thanks for the input, the problem is when using $timeout. That snippet of
code I showed you is not using $timeout so that's probably why you don't
see anything wrong
On Friday, May 2, 2014 3:47:31 AM UTC-4, Marcos Lin wrote:
>
> Your DataFactory looks ok. Could you elaborate on why "sometimes data
> doesn't load in a child controller"? Based on your question, it seems that
> you are not calling DataFactory in your $timeout. Given that you are using
> a "child" controller, perhaps some of the action can be done at the parent
> controller?
>
> Use of $timeout with 0 is fine as long as you know why it is needed. If
> you are doing it just because it works now, it might break after you go
> prod and you would end up with serious bug.
>
>
> On Friday, 2 May 2014 02:25:35 UTC+2, Billy Figueroa wrote:
>>
>> I am already doing that. We are focusing too much on the factory method.
>> My question is regarding the $timeout method
>>
>> 'use strict';
>>
>> mintApp.factory('DataFactory', ["$http", function($http) {
>>
>> var factory = {};
>>
>> factory.getUserData = function() {
>> return
>> $http.get('../MINT/scripts/php/get_user_data').then(function(results) {
>> if (results.status === 200) {
>> return results.data.User;
>> } else {
>> return {
>> status : results.status,
>> error : 'Failed to get User Data'
>> }
>> }
>> });
>> };
>>
>> return factory;
>> }]);
>>
>> and then in the controller I use it
>>
>> DataFactory.getUserData().then(function(results) {
>> $scope.account = results;
>> });
>>
>>
>>
>> On Thursday, May 1, 2014 7:31:20 PM UTC-4, Jeff Hubbard wrote:
>>>
>>> It sounds to me like DataFactory should return a promise instead.
>>>
>>> On Thursday, May 1, 2014 1:13:44 PM UTC-7, Billy Figueroa wrote:
>>>>
>>>> I do have a service. Its called DataFactory.
>>>>
>>>> I just found that you can achieve the same results with out using a
>>>> factory and calling the factory in each controller.
>>>>
>>>> The way I have my app setup I am getting the current users data in a
>>>> MainController which I set on the body tag so EVERY single page and
>>>> controller instantiated with that route will have MainController as a
>>>> parent.
>>>>
>>>> I could then load the data using $timeout OR as you mentioned just use
>>>> a service. Using a service means I call my DataFactory in every single
>>>> controller that I need that data which although it seems like thats the
>>>> point of services and dependency injection, it causes multiple calls of
>>>> the
>>>> same code. Whereas with a simple timeout I can just grab the data that is
>>>> already there in the parent controller.
>>>>
>>>> I am using the service/ factory method for sure, but I just wanted to
>>>> know if its bad practice to do it the other way
>>>>
>>>> On Thursday, May 1, 2014 4:06:43 PM UTC-4, Chris Rhoden wrote:
>>>>>
>>>>> The 0 isn't necessary, and using $timeout to defer some action isn't
>>>>> an antipattern, but the rest of your message concerns me. If you're
>>>>> communicating between controllers using $scope, you're doing it wrong.
>>>>> Make
>>>>> a service if two controllers need to share state.
>>>>>
>>>>>
>>>>> On Thu, May 1, 2014 at 4:04 PM, Billy Figueroa <[email protected]>wrote:
>>>>>
>>>>>> Is is bad practice to use $timeout with a time of 0?
>>>>>>
>>>>>> i.e.
>>>>>> $timeout(function() {
>>>>>> // some action here
>>>>>> }, 0);
>>>>>>
>>>>>> I find that sometimes data doesn't load in a child controller and I
>>>>>> may want to use this. I have avoided using it, but what are peoples
>>>>>> experience with this?
>>>>>>
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "AngularJS" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> To post to this group, send email to [email protected].
>>>>>> Visit this group at http://groups.google.com/group/angular.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> chrisrhoden
>>>>>
>>>>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.