Cakephp - automatically run app model callback with models callback - cakephp

I have callback functions in AppModel.php, and also for some models, is there is way to automatically call the app model callback with (preferably before) the current model's callback is being called.
e.g. lets say I have beforeSave in AppModel, for each beforeSave function in my models I have to put
parent::beforeSave($options) in it. Now, can I make it at once for all models, so I will not have to put in each callback in each model.
Thanks

Nope.
Because your Model extends AppModel, any of these callback functions will override the parent function. You will always have to manually call the parent function.

Related

How To refetch all the model inside a model in just one trigger without using loop

Hi I have question regarding refetching of the model on backbone
is there a way to fetch all model in a model
basically if i have my model and if that said model has an attribute of model as well i could trigger a re fetch and all the model inside that model will be re fetched
my current process is i would loop all through the attribute of a model and look for a model if i could find any i would fetch it. do you have a better way to do things? any suggestions?
This is my current code
_.each(self._getModels(entityRecordModel.attributes), function (model)
{
model.fetch();
});
The pub/sub pattern can help you here.
When you first initialised the attribute models, just subscribe to the main model's sync event and then fetch them as well.
If you want to to all this in one request, then use model's parse method to initialise/reset the attribute models.

Events not firing with Marionette

I'm creating a simple page using a CompositeView from Marionette, with a parent lodash template that wraps a group of templates: one for each item in its Collection.
The page displays properly, and each ItemView renders properly with it's own child template, but binding a 'click' event fails to register anywhere in the application, even if moved into the CompositeView.
I have my suspicions that this is happening due to the events being bound before the element is created, and it not updating afterwards. From what I have read, it uses / used .live() / .on(), but I can't see any other reason that it wouldn't register.
Is there a way to make sure that events are rebound with onRender()? Am I doing something else wrong entirely?
Environment: RequireJS, BabelJS (es6), Backbone 1.1.2, Marionette 2.4.1, jQuery 1.10.2
I've created a gist of the code in question.
Non ES6: Try assigning this.events before the parent constructor call, in Backbone 1.2.1+ the events are created BEFORE initialize in the parent constructor. In 1.1.2 they are created AFTER initialize, but still as the last thing the parent constructor does - so you should either assign them in initialize, or do it before the super call (I recommend before super call in the constructor so that if you upgrade it doesn't break again).
In ES6 you can't use this before the super call.
You could call this.delegateEvents(); again, but it's not ideal as it stops listening and then re-listening to any existing events which is less efficient (though the impact is probably negligible).
You can use es7's static property on the class definition if you are using a transpiler that supports it (e.g. Babel), or make events a function which returns an object if you need it to be dynamic.

Component and beforeFilter not loaded in controller when called from another controller

I'm calling controller2 in controller1 to execute some action. Problem is, controller2's action requires a call to its beforeFilter and a Component that is loaded in AppController.
Actually, controller2's action executes well when called directly but I get errors when called in controller1 as follows:
App::import('Controller', 'Second');
$Second = new SecondController;
$Second->someAction();
Variables that are defined in the Component and in the beforeFilter appear unset.
How could I load an external controller with calling beforeFilter and using my custom Component?
In that case there are few options
1. Use request Actions http://book.cakephp.org/2.0/en/controllers.html#Controller::requestAction
2. Move the logic or code you want to execute from SecondCrontroller to common component and use it in both contrllers.
3. Move the logic or code you want to execute from SecondCrontroller to AppController

Don't call render() if the View itself updated Model via Backbone.Model.set()

In my Backbone.js project I have one Model and several Views. All Views have registered callbacks for 'change:currentTextTitle' on this model:
// 'this' stands for any of the Views here
myModel.on('change:currentTextTitle', this.render, this);
Now a user performs some action, which causes the specific View to change its "current text title" field. This specific view then calls myModel.set("currentTextField", newTextValue) which in turn triggers the 'change:currentTextTitle' event calling all Views (including the one from which set() originated). Then all Views call their render callback functions.
The problem is that the render method is also called on the View from which the set()-Method was originally called, which is completely unnecessary because it is already up-to-date with currentTextTitle.
How would my Views call myModel.set() in a way that the other Views' callbacks get informed, but without triggering/calling the "source View" itself?
One workaround seems to be to pass the source view as part of the options parameter of the set() method (which gets passed along to trigger() and then along the the render() callback):
myModel.set("currentTextField", newTextValue, thisViewSetAttribute)
Then in the render callback one could check if thisViewSetAttribute != this. However, instead of implementing checks in every callback, I think it would make more sense to handle this in the Model itself by only calling the necessary callbacks and ignoring the source View from which the set() method call originated. Is this possible?
I think the 'proper' MCV solution is that your views should not know or care how the model changed, they should simply handle the change and update accordingly. If they are already current, the user shouldn't know the difference.
I definitely would not pass the source view to the model. Instead when the model changes, you could just have the view check if it is current and not re-render. But if the extra render doesn't cause any issues then just let it happen :)
In Backbone, the 'view' is both view and controller. So try to treat the change as 2 separate steps. First, convert user input into changes on the model, then as a separate step (initiated by model change event), handle that change and update the view. If each view does this, no matter how the model changes, everything will stay up-to-date.

Is it good practice to override fetch() and save() directly from the Model?

I don't want to override Backbone.sync() as some of my models will actually use the standard sync().
Is it good practice to override fetch() and save() directly from the Model?
It is not necessary to override sync globally. You can do it per Model/Collection ie,
var MyModel = Backbone.Model.extend({
sync: customSync,
...
});
This avoids overriding Backbone.sync globally.
Now if you do not need to implement a full sync, for instance it you only need to override fetch, you can of course do so on a per model basis too.
Presumably you have seen Backbone.sync being overriden by the localStorage version. This is indeed not necessary (I would think it is bad practice). It is sufficient to just define the custom sync function and let models/collections use it.

Resources