I am currently working on a project where I have a large array of objects that need to be filtered, processed, and then displayed to the user. The application is built using node-webkit
and I am using AngularJS
and jQuery
for routing, DOM manipulation, and other functionalities.
Here is the .state configuration:
.state('foo.bar', {
url: '/fooBar',
templateUrl: 'js/Modules/FooBar/Templates/FooBar.html',
controller: FooBarController,
})
The data for the objects is stored in FooBarData.js:
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
}, {}, {}, ];
}
I have hit a roadblock when it comes to filtering based on the source
property and displaying only the relevant information in the views. I have attempted to incorporate filtering logic within the .state using resolve
, but I am unsure of how to access the filtered data afterwards. Here is how the updated .state looks:
.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 challenge arises when attempting to instantiate and 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 create new copies of the filtered objects for retrieval? I feel like I might be overcomplicating things. If you agree, what would be a simpler approach?
- Is it better to utilize FooBarController (currently empty) instead of FooBarFactory? And would the process for accessing objects remain the same?
- When displaying a property like
fooName
of an object, should I use
?{{FooBarFactory.fooStuff.fooName}}
Note: I believe this post does not fit CodeReview as I have some issues I am trying to tackle, and I have provided as much detail as possible.