Useful Tips - swimlane/angular-model-factory GitHub Wiki

Usage

This system DOESN'T make sense for all your $http assets. I'd recommend implementing for assets that have CRUD with RESTful APIs.

Cache

Angular caches the http response from the server in a $cacheFactory based on the url of the request. Angular does not handle cache invalidation though. During a POST/UPDATE/DELETE $modelFactory actually will invalidate the cache using the invalidateCache factory. This will remove ALL cache instances for that particular model.

If you choose to use the cache, you should also consider other clients invalidating your cache. This can be achieved by using a socket implementation at the server level to distribute events to the client to invalidate cache. $modelFactory keeps an instance of the $cacheFactory on its static instance for easy access to do your invalidation.

Event Distribution

Sometimes you need a pub/sub model. Using Angular's core broadcast system we can achieve that relatively simple.

var factory = $modelFactory('api/zoo', {
    actions:{
        'delete': {
            afterRequest:function(model){
                $rootScope.$broadcast('animalDeleted', model.id);
            }
        }
    }
});

then later in another controller/etc:

$rootScope.$on('animalDeleted', function(id){
    alert('Animal deleted: ' + id)    
});

Button States

The $pending attribute on the model can be used to easily disable a button while things are saving/updating/deleting. Example:

<button ng-disabled="myModel.$pending">Save</button>

when completed the $pending state will be set to false re-enabling the button.

Prefixes

Prefixes allow you to set global url prefixes. If defined ALL models will inherit this prefix unless you override it in the options OR use the custom overrideUrl property in the action.

⚠️ **GitHub.com Fallback** ⚠️