An Angular JS flux expansion based on experiences building www.jsfridge.com and www.jflux.io. Read more about FLUX over at Facebook Flux. I wrote an articles about it: My experiences building a FLUX application and [Is it possible to use the FLUX architecture with Angular JS?] (http://www.christianalfoni.com/javascript/2014/09/25/using-flux-with-angular.html)
It can be difficult to get going with FLUX as there is no complete framework with all the tools you need. This project will help you get going with the FLUX parts.
Download from releases/ folder of the repo, use npm install flux-angular or bower install flux-angular.
v1.3.1 - Added ArrayBuffer, Blob and File as "non-clonable" objects
v1.3.0 - Removed buffer dep, smaller file size - Cloning data passed to actions also, to avoid mutation - Removing Angular hashKeys passed to the store
v1.2.0 - Got rid of state object alltogether to keep a more consistent syntax. There really is no need for a special state object. If you want it, create it :-)
angular.module('app', ['flux'])
.factory('actions', function (flux) {
return flux.actions([
'addTodo',
'removeTodo'
]);
});Use them inside controllers or other parts of your architecture. The only way to change the state of your application is through an action.
angular.module('app', ['flux'])
.factory('MyStore', function (flux) {
return flux.store();
});Creates a store.
angular.module('app', ['flux'])
.factory('MyStore', function (flux) {
return flux.store({
todos: []
});
});Just add properties to your store.
angular.module('app', ['flux'])
.factory('MyStore', function (flux, actions) {
return flux.store({
todos: [],
actions: [
actions.addTodo
]
});
});List what actions the store should handle. They will map to a handler with the same name.
angular.module('app', ['flux'])
.factory('MyStore', function (flux, actions) {
return flux.store({
todos: [],
actions: [
actions.addTodo
],
addTodo: function (title) {
this.todos.push({title: title, created: Date.now()});
}
});
});Based on the name of the action, add a handler that will run when the action is triggered. Any arguments passed to the action will be available in the handler.
angular.module('app', ['flux'])
.factory('MyStore', function (flux, actions) {
return flux.store({
todos: [],
actions: [
actions.addTodo
],
addTodo: function (title) {
this.todos.push({title: title, created: Date.now()});
},
exports: {
getTodos: function () {
return this.todos;
}
}
});
});Exports are the GETTER methods used by your controllers to extract state from the store. The returned values are automatically deep cloned to keep the store immutable. The method context is bound to the store itself.
angular.module('app', ['flux'])
.factory('MyStore', function (flux, actions) {
return flux.store({
todos: [],
actions: [
actions.addTodo
],
addTodo: function (title) {
this.todos.push({title: title, created: Date.now()});
this.emitChange();
this.emit('added');
}
});
});Run emitChange to update all bound scopes about a change in the store. Run emit with a named event to notify controllers to trigger something. In this example, maybe you wanted to play an animation in a controller whenever a todo was added.
Note! When emitChange is run all values on state will be cloned to bound scopes. Meaning that the state of a store is immutable. You can not do changes to a bound value on a scope and expect that to be valid inside your store also. You have to trigger an action to change the state of a store.
angular.module('app', ['flux'])
.factory('MyMixin', function (actions) {
return MyMixin = {
actions: [
actions.removeTodo
],
removeTodo: function (index) {
this.todos.splice(index, 1);
this.emitChange();
}
};
})
.factory('MyStore', function (flux, actions, MyMixin) {
return flux.store({
mixin: [MyMixin],
todos: [],
actions: [
actions.addTodo
],
addTodo: function (title) {
this.todos.push({title: title, created: Date.now()});
this.emitChange();
},
exports: function () {
getTodos: function () {
return this.todos;
}
}
});
});Mixins helps you handle big stores. You do not want to divide your stores within one section of your application as they very quickly become dependant on each other. That can result in circular dependency problems. Use mixins instead and create big stores. mixins, actions and handlers will be merged with the main store.
ProTip! In big stores it is a good idea to create a StatesMixin that holds all possible state properties in your store. That makes it very easy to look up what states are available to you.
angular.module('app', ['flux'])
.factory('StateMixin', function () {
return {
someState: true,
stateA: 'foo',
stateB: 'bar',
stateC: []
};
})
.factory('MyStore', function (flux, StateMixin, OtherMixin, ThirdMixin) {
return flux.store({
mixin: [StateMixin, OtherMixin, ThirdMixin]
});
});angular.module('app', ['flux'])
.factory('actions', function (flux) {
return flux.actions(['addTodo']);
})
.factory('MyStore', function (flux, actions) {
return flux.store({
todos: [],
actions: [actions.addTodo],
addTodo: function (title) {
this.todos.push({title: title});
this.emitChange();
},
exports: {
getTodos: function () {
return this.todos.filter(function (todo, index) {
return index < 5; // Just get the 5 first todos
});
}
}
});
})
.controller('MyCtrl', function ($scope, MyStore, actions) {
MyStore.bindTo($scope, function () {
$scope.todos = MyStore.getTodos();
});
$scope.title = '';
$scope.addTodo = function () {
actions.addTodo($scope.title);
};
}); <div ng-controller="MyCtrl">
<input type="text" ng-model="title"/>
<ul>
<li ng-repeat="todo in todos">{{todo.title}}</li>
</ul>
</div>When binding a $scope to a store you use a callback to extract the state needed for that specific $scope. The callback will run whenever that store runs an emitChange. This gives a clear definition of what is on your $scope and it allows for grabbing a subset of state, f.ex. filtering.
angular.module('app', ['flux'])
.factory('actions', function (flux) {
return flux.actions(['addTodo']);
})
.factory('MyStore', function (flux, actions) {
return flux.store({
todos: [],
actions: [actions.addTodo],
addTodo: function (title) {
this.todos.push({title: title});
this.emit('todo:added');
this.emitChange();
},
exports: function () {
return this.todos;
}
});
})
.controller('MyCtrl', function ($scope, MyStore, actions) {
MyStore.bindTo($scope, function () {
$scope.todos = MyStore.getTodos();
});
$scope.title = '';
$scope.listClass = {'list': true, 'animation': false};
$scope.$on('todo:added', function () {
$scope.listClass.animation = true;
});
$scope.addTodo = function () {
actions.addTodo($scope.title);
};
});Events emitted in a store will reach all active controllers in your application. Use them to trigger behaviour in controllers that are not realted to reflecting a state value in a template. Listeners does NOT trigger a digest loop so be sure to triggers these before running your emitChange.
flux-angular is licensed under the MIT license.
The MIT License (MIT)
Copyright (c) 2014 Brandon Tilley
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.