modellist
v0.4.6
Published
ModelList
Downloads
17
Readme
angular-modellist
Why?
When storing model data in a service, you most likely will need to bind to it in your view(s). If we bind this data in multiple places we run the risk of loosing the instance if we aren't careful.
var myCtrl1 = function(myService) {
$scope.arrayData = myService.data;
};
var myCtrl2 = function(myService) {
$scope.arrayData = myService.data;
$scope.getNewData = function() {
$http.get("someUrl").success(res) {
$scope.arrayData = res; // This causes us to loose our reference
};
};
};
This service provides an object that wraps an array, but always keeps the same instance when operations are performed on it.
// myService.data = new ModelList();
var myCtrl1 = function(myService) {
$scope.arrayData = myService.data;
};
var myCtrl2 = function(myService) {
$scope.arrayData = myService.data;
$scope.getNewData = function() {
$http.get("someUrl").success(res) {
$scope.arrayData.overwrite(res); // Now our other controls arrayData updates with this one!
};
};
};
How To Use:
####AngularJS####
Just include the module into your app:
angular.module("myApp", ["ModuleList"]);
Inject:
angular.module("myApp", ["ModelList"]).factory("myService", function(ModelList, $http) {
var myService = new ModelList();
myService.update = function() {
$http.get("myUrl").success(function(res) {
myService.overwrite(res.data);
});
};
return myService;
})
.controller("myCtrl", function($scope, myService) {
$scope.myService = myService;
})
.controller("myOtherCtrl", function($scope, myService) {
$scope.myService = myService;
});
Use the getBindableList
method to bind to directives.
<div ng-controller="myCtrl">
<div ng-repeat="item in myService.getBindableList()">
</div>
<button ng-click="myService.update()">
</div>
<div ng-controller="myOtherCtrl">
<div ng-repeat="item in myService.getBindableList()">
<button ng-click="myService.update()">
</div>
Both of the ng-repeat
directives will be in sync when the list is updated.
####NodeJS####
var ModelLIst = require("modellist");
var list = new ModelList();
####Browser####
The ModelList
class will be available on the window object
var list = new ModelList();
API
####Native methods:#### Most array methods are supported in their native form except a couple:
join
pop
push
reverse
shift
unshift
splice
sort
forEach
: Uses a minimal implementation if not supportedsome
every
indexOf
: Uses a minimal implementation if not supportedlastIndexOf
reduce
reduceRight
map
:(Chainable)
Mutates the array and does not return a new arrayconcat
:(Chainable)
Mutates the array and does not return a new arrayfilter
:(Chainable)
Mutates the array and does not return a new arrayslice
:(Chainable)
Mutates the array and does not return a new array
If your browser doesn't support the method natively, it won't be available.
####Custom methods:#####
get(Number:index)
: Gets an item at indexset(*, Number:index)
:(Chainable)
Sets an item at indexclean()
:(Chainable)
Empties the arrayoverwrite(Array:array)
:(Chainable)
Overwrites the array with the items in the new arrayclone()
: Returns a new ModelList object with a cloned listgetBindableList()
: Returns the array. Only should be used for bindingpull(*)
:(Chainable)
Removes object instances from the arraymerge(Array:list, [Object:options])
:(Chainable)
Merges an array of objects with the list of objects. Useful for merging API responses while keeping the same object references.[Function|String:options.comparator]
: The comparator is used for matching 2 objects together. It can be an ID key or a custom compare function.[Function:options.merger])
: An optional merger function can be used to perform the merge. If omitted a default objectextend
will be performed.[Function:options.accumulator]
: An optional function to handle non matched elements. If omitted it will perform a splice of the element at it's position. If you transform the value in this function you must return it so theModelList
can keep track of it.[Function:options.remover]
: An optional function to handle elements in the list but not in the response. If omitted it will be a noop function.
A length
property is also kept in sync just like native array behaviour.
####Static methods:####
create(Array:array, [Boolean:clone])
Returns a new instance of ModelListconvert(Object:object, [Boolean:deep])
Converts all array instances into ModelList instances