I have a big array of objects that need to be filtered, processed and then displayed to the end user. The application is developed on top of node-webkit and I use AngularJS, jQuery for routing, DOM manipulation etc.
Here is the .state:
.state('foo.bar', {
url: '/fooBar',
templateUrl: 'js/Modules/FooBar/Templates/FooBar.html',
controller: FooBarController,
})
FooBarData.js contains the big array of objects:
angular.module('fooApp').factory('FooBarData', function () {
var fooBarArray = [{
source: "foo",
id: 'foo_1',
pid: 'fooBar_1',
number: 550,
fooSign: "ASD",
fooName: "XCV",
url: "../foo/bar.fbr",
aFlag: false
}, {}, {}, ];
}
Where am I stuck? Well, the source property is either foo or bar and I will switch between the two views: when I press foo, I will only display properties related to the objects that are foo and vice-versa.
I have tried sneaking the filtering part inside .state using resolve, but I don't know how to access them after that. Thus, the .state would look like this:
.state('foo.bar', {
url: '/fooBar',
templateUrl: 'js/Modules/FooBar/Templates/FooBar.html',
controller: FooBarController,
resolve: {
Foo: function (FooBarFactory, FooBarData) {
var fooArray = [];
$.each(FooBarData, function (index, value) {
if (value.filter == 'foo') {
var aFoo = new FooBarFactory.FooStuff(value);
fooArray.push(aFoo);
}
});
return fooArray;
},
Bar: function (FooBarFactory, FooBarData) {
var barArray = [];
$.each(FooBarData, function (index, value) {
if (value.filter == 'bar') {
var aBar = new FooBarFactory.BarStuff(value);
barArray.push(aBar);
}
});
return barArray;
}
}
})
The problem appears when I try to instantiate (and then access) FooStuff() and BarStuff() from FooBarFactory:
angular.module('fooApp').factory('FooBarFactory', function ($scope) {
var fooStuff = function (foo) {
this.source = foo.source;
this.id = foo.id;
this.pid = foo.pid;
this.number = foo.number;
this.fooSign = foo.fooSign;
this.fooName = foo.fooName;
this.url = foo.url;
this.aFlag = foo.flag;
};
/*var barStuff = function (bar)
same thing here*/
return {
fooStuff(FooBarData.fooBarArray): fooStuff,
BarStuff(FooBarData.fooBarArray): barStuff
}
});
- Should I retrieve the filtered objects by creating new copies of them? I feel like I'm complicating things too much. If you agree with that, how should I do it instead?
- Should I use the FooBarController (which is empty at the time of speaking) instead of FooBarFactory? If yes, will the process of accessing the objects be the same?
- If I need to display, for example, the property
fooNameof an object, will I use{{FooBarFactory.fooStuff.fooName}}?
Note: I don't think this post should be submitted to CodeReview, I have quite a few issues, yet I tried to be as specific as I can.