I'm new both to AngularJS and JavaScript and probably most people learning
Angular have more JavaScript experience than I do.
I'm trying to use a simple JavaScript pattern for creating pseudo classes
using JavaScript funcitons that I've learned with a pattern for Angular
services from the Angular docs.
Sadly they don't go together and I am left baffled since I don't understand
enough about the internals of JavaScript that seem to govern the hackish
seeming ( to me ) nature of either of these patterns to trouble shoot it
myself. But I'm gathering that there must be a fairly easy way to have
pseudo classes in Angular that most people probably don't see the need to
explain.
So my pseudo class looks like this:
function Node( data) {
this.node_data = data;
this.status = 'NEW";
this.doSomething = function (param) {
if (param == 'SOMETHING')
return "RESULT1";
else
return "RESULT1";
};
this.getCSSClass = function() {
if (this.status == "NEW")
return 'new';
else return 'old';
};
};
so can pass parameters to a pseudo constructer and my class fields and
methods can be accessed under this
this works fine for me when I use it in a controller. In angular there are
multiple patterns to implement a controller, but I am using this:
myApp.controller('NodeCtrl', ['$scope','$rootScope' ,function($scope,
$rootScope)
{
$scope.some_var = null;
$scope.someScopeMethod = function() {
console.log('scope did something');
};
function Node( data) {
this.node_data = data;
this.status = 'NEW";
this.doSomething = function (param) {
if (param == 'SOMETHING')
return "RESULT1";
else
return "RESULT1";
};
this.getCSSClass = function() {
if (this.status == "NEW")
return 'new';
else return 'old';
};
};
$scope.someOtherScopeMethod = function() {
console.log('scope did something else');
};
}]);
So $scope holds the scope for my Angular related stuff and I have class
whose scope is referenced by this within the class
This works fine, its when I want to repeat this class pattern with in a
service instead of a controller that I get lost:
There are various patterns for a service but I am using this factory method:
myApp.factory('nodeService', function($rootScope) {
var my_service_var1 = null;
var my_node_list = [];
return {
aServiceMethod: function(param) {
console.log('doing something in my service');
},
Node:function(data) {
this.node_data = data;
this.status = 'NEW";
this.doSomething = function (param) {
if (param == 'SOMETHING')
return "RESULT1";
else
return "RESULT1";
};
this.getCSSClass = function() {
if (this.status == "NEW")
return 'new';
else return 'old';
};
},
anotherServiceMethod: function(param) {
console.log('doing something else in my service');
}
};
}
);
So here the factory function is returning the service methods as a JSON
object, which is not something I'm farmiliar with in JavaScript.
I'm just copying this pattern from ANgularJS's docs and don't understand
exactly why its causing trouble.
What happens is my class scope referenced by this within the Node class
references both the class variables I expect it to but it also
references the service variables and methods, which I do not want. I want
the Node class to have its own seperate scope from everything else.
So my question is how to do this using the factory method for a service?
It may something as simple as defining the Node pseudo class outside of the
factory method definition for my service and somehow referencing or
injecting the
Node pseudo class into the service so I can use it there, but I haven't
been able to find if this is possible as it seems to be a hard question to
frame given my inexperience
in a way that a search engine understands.
I'm starting to understand Angular's architecture and it seems quite well
thought out,
but less well thought out is how to explain it to people like me where the
architecture seems to make the simple creation of a class into a problem I
can't solve myself.
Anyone that wants to add some advice or philosophy about using pseudo
classes like I am trying to is welcome to educate a willing noob.
I am basically accessing REST endpoints to initialize different items
stored in pseaudo classes like my node class
and having some functionality built into the class as well as passing say a
list of these objects to ng-repeat for display.
I guess the alternative to this approach is just storing the items as JSON
objects with no class wrapper and having the functionality in stand alone
methods that
take the item as a JSON object as a parameter.
Using classes seems more powerful but maybe this is just a shift I have to
make when using Angular?
Doug
--
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.