angular-property-binder
Tiny library which help you to create reference variables and keep them intact.
Don't use you're controller as a MODEL - Best Practice.
Controllers should bind references to Models only (and call methods returned from promises)
Installation
bower install angular-property-binder
npm install angular-property-binder
Simply add:
<script type="text/javascript" src="dist/js/angular-property-binder.min.js">
</script>
to your HTML, load module ['PropertyBinder']
and then inject the binder service to your controller|service|factory|provider
angular.module('MyApp', ['PropertyBinder'])
.controller('MyController', [
'PropertyBinder.services.binder',
function(bind) {
...
}])
API Reference
Angular Property Binder service provides easy to use and minimalistic chaining methods.
Here is the full list of accessible methods:
Service [PropertyBinder.services.binder]
###binder( value )
Params:
- value: String or Array. property name to bind.
Returns:
Methods
###from( scope, path )
Sets the source of the property to bind
Mandatory
Params:
- scope: Object or Array. The source object containing the property to bind.
- path: String or Array. The path to the targeted source.(optional)
Useful when you wan't to create a reference on a nested property
Returns:
###to( scope )
Sets the target of the property to bind
Mandatory
Params:
- scope: Object or Array. The targeted object.
Returns:
###apply( )
Applies the configured binding
Mandatory
Returns:
###as( alias )
Sets the alias to use for the binding
Optional
Params:
- scope: Object or Array or String. The alias(es) to use.
Returns:
###onchange( callback )
Sets a onchange event
Optional
Params:
- callback: Function. The function called each time the property is updated from the created reference.
Returns:
###seal( )
Seals the binding.
The next assignations will not work after this.
Returns:
###unseal( )
Useal the binding.
The next assignations will now work.
Returns:
###destroy( )
Delete the created reference
=======================
Basic usage:
See the full documented example in example/modules/app/..
.
angular.module('App')
.controller('App.controllers.appTest', [
'PropertyBinder.services.binder',
'App.factories.users',
function(bind, userFactory){
bind('users')
.from(userFactory)
.to(this )
.apply();
console.log(this.users);
bind('eyeColor')
.from(userFactory, 'users.0')
.to(this)
.apply();
bind('nbToLoad')
.from(userFactory)
.to(this)
.apply()
.onchange((newVal, oldVal) => {
console.log('property updated', newVal, oldVal);
userFactory.load();
});
var binding = bind('users').as('usersAlias').from(userFactory).to(this ).apply();
binding.seal();
binding.unseal();
bind('load').to(this).from(userFactory).apply();
userFactory.load();
}]);
Other:
var app = angular.module('app', ['PropertyBinder']);
app.factory('app.factories.user', function() {
return {
authenticate: function() {},
update: function() {},
register: function() {},
user: {
name: 'Jack',
firstname: 'Bar',
location: {
city: 'Fu',
country: 'FuBar'
}
}
};
});
app.controller('app.controllers.sample', [
'$scope',
'app.factories.user',
'PropertyBinder.services.binder',
function($scope, userFactory, bind) {
bind('user').from(userFactory).to($scope).apply();
console.log($scope.user.firstname);
var binding = bind('firstname').as('userName').to($scope).from(userFactory, ['user']).apply();
console.log($scope.userName);
$scope.userName = 'FuFuBarBar';
console.log($scope.userName, userFactory.user.firstname, $scope.user.firstname);
binding.seal();
$scope.userName = 'Bar';
console.log($scope.userName, userFactory.user.firstname, $scope.user.firstname);
$scope.user.firstname = 'Bar';
console.log($scope.userName, userFactory.user.firstname, $scope.user.firstname);
binding.unseal();
bind(['city', 'country']).to($scope).from(userFactory, 'user.location').apply();
bind('register').from(userFactory).to($scope).apply();
binding.destroy();
}]);
Modify and build
npm install
To build the dev version just type: grunt es6
It will create js files from the es6 sources.
To build the dist version just type: grunt build
It will generate and minify js files.