I would like to have two-panel filemanager like mc or total commander. Do I have to create two different controllers with same functionality?
Probably you can just use two instances of the same controller. Something like:
new App.FileManager({el: $('#panel_left')});
new App.FileManager({el: $('#panel_right')});
Obviously, it's just a general idea.
Related
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.
I have splitted my application into two main areas.
Part(A)
PartStashContainer(B)
The content of A should be set based on what user wants.
So basically i can have 1..N classes which could be used in Class URI of Part in application model.
I don't know if i should replace the whole Part(A) with new dynamically created Part(C) which has content i want, or i should somehow to modify the existing Part (call setContributionURI, or setObject methods on Part object?).
It does make more sense to me to modify the existing Part, because it is defined in Application model and therefore already describing the location where the content should be.
Possible solutions:
Modify the Part object so it "reload" its content based on new setup (But how? Can setContributionURI or setObject methods help?)
Remove the old Part and add dynamically on same place in Application model the new Part (using EModelService and EPartService).
other solution??
If you want to reuse the Part then do something like:
MPart part = find or inject your part
MyClass myClass = (MyClass)part.getObject();
... call a method of MyClass to change the contents
MyClass is the class you specify for the object in the application model. You should add a method to that to let you change the contents.
Don't try to call setObject, this is really only for use by Eclipse. I don't think setContributionURI would do anything after the part is created (but I am not sure).
If you want to use different classes for the different data then you really should use different Parts.
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.
I would like to make a global Zend_Log object that I can reach from my Controllers and my Models.
What should I add to my Bootstrap? (My bootstrap extends Zend_Application_Bootstrap)
How then can I reach the logger object from my controller actions and from my model?
As you do with any other class - assign it to the Zend_Registy. I'd suggest setting like this:
Zend_Registry::set('Zend_Log',$logInstance);
This is a common way, which is used also for translate (set translate instance to 'Zend_Translate' and classes like forms and validators will find it automatically).
You can use Zend_Registry::get('Zend_Log')->log(...) to log anywhere you want. It's not very good from the point of architecture (you should not use normally), but for log - which can appear practically anywhere in the app from view helpers to controllers and models it's a good thing.