These are chat archives for jdubray/sam-examples

19th
Oct 2016
Nadal Gonzalo García Zavala
@snorkellingcactus
Oct 19 2016 13:45

Hi. In the rocket launcher example, ¿What is the purpose of this line?

present = present || model.present ;

When i would want to pass a custom present function? in which cases?

Another question: It's a bad idea to deal with a model directly from an action instead of passing a associative-array like data to model.present() ?

For example:

function actionsLaunch()
{
    model.start()        //Set model->started member to true
    model.present()
}

Instead of:

actions.launch = function(data, present) {
    present = present || model.present ;
    data.launched = true ;
    present(data) ;
}

I'm trying to port it to PHP, and see how it look in an Object-Oriented like style.

Thanks you

Jean-Jacques Dubray
@jdubray
Oct 19 2016 13:56
The actions can be rewired on the spot rather than using the default present method (of the model). It can also be used for unit testing because the action does not return a value, SAM is based on a reactive loop: event->(action->model->state)->view
I'd say, yes in general it's better to not factor the model around "actions" but rather around "units of work". We are used (as humans and developers) to think in terms of actions, but your code is better factored when the actions sit outside the model and the model is more "data centric", a little bit like a database. You talk to the model in terms of SQL statements. There is a bit of logic in the database but that logic is focused on the data integrity, not knowing why the data got there in the first place. That's the role of the action.