I have added user accounts and a few other things:
http://cartflux.meteor.com
-
User Accounts, with a new store (UserStore) to control logins, account creation, errors…
-
UserActions, for login/logout actions. This pattern is suggested by Facebook for async actions:
For example, instead of dispatching the actions in the Template events, you use the UserActions:
Template.LoginForm.events({
'submit #login-form': function(event){
event.preventDefault();
UserActions.login(event.target.user.value, event.target.password.value);
},
and then…
UserActions = {
login: function(user,password){
Meteor.loginWithPassword(user, password, function(error){
if (error) {
Dispatcher.dispatch({ actionType: "LOGIN_FAILED", error: error });
} else {
Dispatcher.dispatch({ actionType: "LOGIN_SUCCEED" });
}
});
},
This is only for async actions.
-
Library to validate inputs in the client. This pattern is suggested by Facebook as well.
To do things like this:
UserActions = {
createAccount: function(user){
if (!Libs.Validations.areValidPasswords(user.password, user.retry_password))
var error = new Meteor.Error("password-doesnt-match", "It looks like the passwords don't match.");
if (error)
Dispatcher.dispatch({ actionType: "CREATE_ACCOUNT_FAILED", error: error });
-
Refactor of the Stores, so now they can be unit tested. I added dependency injection as well.
It’s very simple, instead of creating the object, you create it inside a function and return it:
// Creator
var newOneStore = function(SomeCollection){
// Reactive Vars
var something = new ReactiveVar(false);
// Object
var OneStore = {
// Callbacks...
onSomethingHappened: function(){
something.set(true);
SomeCollection.insert({});
},
// Getters...
getSomething: function(){
return something.get();
}
};
OneStore.tokenId = Dispatcher.register(function(payload){
switch(payload.actionType){
case "SOMETHING_HAPPENED":
OneStore.onSomethingHappened();
break;
}
});
return OneStore;
};
// Create the instance
SomeCollection = new Mongo.Collection('some-collection');
OneStore = newOneStore(SomeCollection);
Of course you can use this pattern or not.