I've just started using Sails and I have a couple models with the same method defined multiple times. Is there a way to share logic between models? Like having a "Base" model from which other models inherit behaviour?
Same question for controllers. Is there a way to setup an "ApplicationController" or a system of inheritance?
Agree with Yedhu, however You can also create defaults and extend using lodash
var = defaultModel = require('./<LOCATION OF DEFAULT MODEL DEFINITION>/defaultModel');
module.exports.models = _.merge({
// .. defign your model here like your normally would
}, defaultModel)
I am new to Sails JS, started just two months back.
Not particularly for Models or Controllers. But still shared logic can go to Services. I use this when I need to access the same code from multiple places within the application.
Related
Using the AMD and module paradigm used by UI5, I want to use separate JS classes in distinct JS files to separate my ajax code from the related controllers.
A foundation class will be concerned with common activity such as generic error handling whilst specific classes extending from this will deal with subject-specific ajax communication only. This will NOT be a custom control so no requirement for render capability, metadata, etc.
I wish to benefit from the sap.ui.define functionality and also want my new class to be good UI5 citizen. For example I want to fire my init when the class is instantiated.
Which sapui5 class or classes should I extend from? I am currently using sap/ui/base/Object but would like to know if there is a better choice based on better performance or better fit to purpose.
I am aware of the documentation on custom controls but this seems to focus only on details of classes that render to screen.
This is the skeleton of my current approach:
sap.ui.define(['sap/ui/base/Object'],
function(BaseObject) {
"use strict";
var AjaxBase = BaseObject.extend("myAjaxBase", {
constructor: function(oControl) {
BaseObject.apply(this);
console.log("AjaxBase.constructor() fires")
}
})
AjaxBase.prototype.init = function() {
console.log("AjaxBase.init() fires")
}
return AjaxBase;
}, true)
You can use sap.ui.define even for objects which do not extend sap.ui.base.Object. So if you do not really need functionality provided by UI5 objects you are not forced to extend them. However, in your case it looks like you want to use event support and in this case it would be useful to at least extend sap.ui.base.EventProvider.
As you want to separate backend calls from your controllers it looks like you are using a JSONModel and have more complex use cases than just reads. In this case you could also extend the JSONModel with support for create, update and delete operations.
I know there are view controllers and action controllers. I think that view helpers can be used from views and action helpers used from actions in controllers.
I need a class that at bootstrap or wherever, it initializes a number of configuration options, arrays for things like convert month numbers to their names and role numbers to their names.
How can this be achieved?
Put them in a model and use it anywhere you like by instantiating it and calling its helper methods. All model files are auto loaded whenever you call them.
Have a model Constants.php:
<?php
class Constants {
public static function convertMonth($month) {
doLogic();
return $something;
}
}
?>
In your controller or view:
Constants::convertMonth(12);
You could build a Resource Plugin and then add it to yout bootstrap class.
The Constants class or Resource approaches both work nicely. However, I recently had to undo/upgrade a Constants class based solution to meet new requirements, so you might want to consider your future plans before going down those paths.
Specifically, if you ever intend to support multiple languages, or even different words for the constants in different contexts, check out Zend_Translate API docs, Zend_Translate example, or this blog post.
In Play! 2.0, one can use Global as documented here. Global needs to be in the default (empty) package.
I also need globals in my application, and some of them need to be available to the methods in Global. Thus, I put them in Global.scala like so:
package object globals {
lazy val foo = Play.maybeApplication.flatMap(_.configuration.getString("foo")).getOrElse("default_value_of_foo")
}
And then I use it in my controllers like this:
globals.foo
Is there a better way?
I think this question is more about general software design than it is about Play Framework. If you truly need a bunch of random properties why not create your own object?
object Configuration {
val timeout = Play.maybeApplication.flatMap(
_.configuration.getString("timeout")
).getOrElse(0))
}
But usually the values belong to some other logical entity that is being configured.
I had problems using Global as a singleton for my app. I think you will have some problems too to access the singleton from your controllers (see this post).
But you can create your own singleton in one of your packages and access it as you plan to do.
How can I eliminate to write $object = new Application_Model_Database() in every controller?
For example for an article controller, I have to type $articles = new Application_Model_Articles() for every controller. Should I put it under viewer controller, action helpers, or any other way?
Your question almost sounds like an OOP best practices question as opposed to a Zend Framework specific question. Regardless of whether or not I'm using a framework, and regardless of what framework I choose, I base when and where I create new objects on testability how many times I have to write $object = new My_Random_Object();.
Speaking specifically to the Zend Framework: Objects I'm going to use everywhere, or almost everywhere, get created in Bootstrap.php. These objects generally include a database adapter, logger, view object, and any plugins I might use. To access these across the application, I'll create private properties in the appropriate controllers and assign the objects to those properties in the controller's init() method.
class ExampleController extends Zend_Controller_Action
{
public function init()
{
$bootstrap = $this->getInvokeArg('bootstrap');
$this->_db = $bootstrap->getResource('db');
$this->_log = $bootstrap->getResource('log');
// and so on, and so forth
}
}
Ideally, models, services, daos, etc, will all be relatively tightly grouped by controller and by action. In my experience, and this is speaking generally, if I have the same model or service class showing up across all of the controllers in my application, I have an organization problem. That being said, any model that shows up in only one action gets created in that action. If it's across actions in a controller, it gets created in the init() method and assigned to a property. If it shows up across multiple controllers, it gets created in my Bootstrap.php.
(Ideally, everything gets created in the Bootstrap.php, so you can swap out that bootstrap for testing purposes. Sadly, I don't always do that, and I most often use the principles I outlined above.)
Well do you really need it in every controllers? Because that's pretty much by design. You implement models when you need them. Its not that much code really.
Now if its to be used across actions from a controller you could always:
class MyController extends Zend_Controllers{
$protected $_articleModel;
...
and in your constructor or __init() function initialize it so you can use it in every action thru $this->_articleModel
If you REALLY want it everywhere in your application just initialize it in your bootstrap and store it in the registry.
public function __initModels(){
$articles = new Application_Model_Articles()
Zend_Registry::set('articles', $articles );
}
And access it in your controllers like so:
Zend_Registry::get('articles')->fetchAll();
But then your still writing a couple of characters.
Hope this help!
IF you want to use models in the controllers you must call it..anyway some shortcuts are here
1.You can initialize it in the init section of your controller like
public function init(){
$this->object = new Application_Model_Database();
}
So that the this->object is available in all the actions of that particular controller
2.Use Zend_registry as suggested in the above answer
Another possibility is to use a Dependency Injection container, such as the Symfony DI component. It takes care of instantiating your objects, and you get some additional benefits:
Separation of concerns. You have a component devoted to create your object tree.
Easier testability of the objects.
Last, but not least, the performance benefits given by lazy instantiation (objects are created only when you ask for them). Thus, if some object is not used by the particular controller serving your request, it's not instantiated).
It's a bit more laborious than the above solutions, but much more flexible if you need to maintain and extend your application in the future.
Hope that helps,
If you are using this object to just display data in your view and are using your controller to grab the data and assign it to your view, like so:
//someControllerAction
$object = new Application_Model_Articles();
$object->fetchAll();
//assign to view
$this->view->articles = $object;
You might be better off making a view helper similar to:
//Articles.php put in /application/views/helpers
class Zend_View_Helper_Articles extends Zend_View_Helper_Abstract {
public function Articles() {
$articles = new Application_Model_Articles();
$articles->fetchAll();
//return rowset object
return $articles;
Then in your view (phtml) you could do something like:
//someView.phmtl
<?php $articles = $this->Articles(); ?>
<h1><?php echo $this->escape($articles->title); ?></h1>
<p><?php echo $this->escape($articles->body); ?></p>
building a view helper allows you to bypass the controller completely if you just need to display data from the model. This is a very simple example and can be used with partials and partialLoops.
REF:ZF reference Custom View Helper
ZF partial view helper reference
I'm learning prism V4 using MEF to load my modules. Loading modules does work, but in one module I want to load a View/ViewModel (MVVM) and don't really know how I get MEF to resolve all this stuff for me.
First: how do I need to mark the ViewModel (I follow the StockTraderRI example) so it is not loaded on startup but instead can be loaded during runtime into a region?
Second: how do I load the ViewModel using MEF so it gets connected to the corresponding interfaces?
MEF does this very nicely for things on startup which are marked as [Export], but I got no idea how to achieve this during runtime.
You can use what is known as a Lazy Export so that the interface is not resolved until you explicitly use it.
If you need to create multiple instances, MEF doesn't support this particularly well. You can either do your own discovery and instantiation, or you can define the Export like this:
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(ISomething)]
public class Something : ISomething { }
The downside is that now wherever you need to create the instance, you need to have a reference to the actual Container instance. Then you can do:
var something = _container.GetExportedObject<ISomething>();
EDIT: Okay, I think I understand better what you're after. Here is how I've typically resolved this issue:
I implement my View objects as UserControl instances and don't set a DataContext anywhere in their code or XAML.
I create a DataTemplate that binds from the Type of the ViewModel to the UserControl.
On my MainViewModel (or whatever corresponds to the View hosting the regions), I expose a general RegionX Object (possibly typed to an interface if all of my ViewModels will share some common functionality, but Object works fine).
I create a ContentPresenter with Content bound to the RegionX property.
Now my MainViewModel can import different ViewModel instances corresponding to the types of ViewModels that might be hosted by the RegionX. When I want to switch the 'active' View in the region, I simply set RegionX to the corresponding ViewModel.