I have an instance of a class (e.g Ext.data.Model) myRecord and need to call one of its static methods (e.g getFields()). How can I do that?
You can also use the self property to get the class:
myRecord.self.getFields();
You need the class of that instance and then simply call the static method.
E.g:
var myClass = Ext.ClassManager.getClass( myRecord );
myClass.getFields();
Related
I have a question, I confuss about when I must use class static.
I understand that I have use class static when I need some methods that going to use many times in the code, and that class doesn´t need to be declarate, but in a example in android I find that code.
Where they declare a static class and call it with an instance...
Why did they do that?
public View getView(int position, View view, ViewGroup viewGroup) {
//View holder pattern
**ViewHolder holder;**
if(view ==null){
LayoutInflater layoutInflater=LayoutInflater.from(this.context);
view=layoutInflater.inflate(R.layout.list_item,null);
**holder=new ViewHolder();**
holder.txtView =(TextView) view.findViewById(R.id.txtView);
view.setTag(holder);
}
return view;
}
**static class ViewHolder{
private TextView txtView;
}**
Thanks for your explanation..
The advantage of having a static nested class over an non static one is, that to create an instance of the static nested class you don’t need an instance of the outer class. If you only have a non static inner class you need an object of the outer one to be able to create an instance.
Note that only nested classes can be static.
I'm using typescript&angularJS for a project and I have a public class (not controller, just a regular class) to hold some enums, is that possible I can instantiate a static member of this class with a service object?
With Controller, it's very easy to use DI of AngularJS to instantiate a service object but for a regular class, I don't know how to do it.
Thanks.
Add code snippet:
export public Enums {
public static serviceObj; //how to instantiate serviceObj with some service?
}
Not exactly sure what you're asking but is this what you're looking for?
interface ServiceObj {
}
class Enums {
public static serviceObj : ServiceObj;
}
Enums.serviceObj = [set from some external location]
I got a ViewModel which I export with MEF. I'd like this ViewModel to be initialized differently each time it's being imported, according to an enum/specific object parameter that will be provided to it.
I've been reading a little on the subject and I found that maybe this -
http://msdn.microsoft.com/en-us/library/ee155691.aspx#metadata_and_metadata_views
would be able to fit my needs, but I'm not sure that this would be the best way to do it.
Another method I've been thinking about is importing the class normally, and then once I've an instance, to call a special initialization method that would receive my parameter. However this doesn't seem like a classic MEF implementation, and maybe losses some of its "magic".
I'm hoping someone would be able to point out for me what would be the recommended method to achieve this.
Thanks!
A workaround is exporting a factory that creates instances of your type. While this means you cannot directly import thos instances, it does have the benefit that the logic to create them is the responsability of the factory so users of the class do not have to know about it:
public class ServiceWithParameter
{
public ServiceWithParameter( int a )
{
this.a = a;
}
private readonly int a;
}
[Export]
public class ServiceWithParameterFactory
{
public ServiceWithParameterFactory()
{
instance = 0;
}
public ServiceWithParameter Instance()
{
return new ServiceWithParameter( instance++ );
}
private int instance;
}
//now everywhere you need ServiceWithParameter:
[Import]
ServiceWithParameterFactory serviceFactory;
var instanceA = serviceFactory.Instance(); //instanceA.a = 0
var instanceB = serviceFactory.Instance(); //instanceB.a = 1
A more extensible way is telling the container you have a factory and an example is presented here: http://pwlodek.blogspot.com/2010/10/mef-object-factories-using-export.html
I'm trying to turn an string into an instance name.
stage.focus = ["box_"+[i+1]];
this gives me back = box_2;
but I need it to be an object not a string.
In as2 I could use eval. How do I do it in as3?
The correct syntax is:
this["box_"+(i+1)]
For example if you would like to call the function "start" in your main class, you'd do it this way:
this["start"]();
Same thing goes for variables. Since all classes are a subclass of Object you can retrieve their variables like you would with an ordinary object. A class like this:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public var button:Sprite;
public function Main(){
trace(this["button"]);
}
}
}
Would output:
[object Sprite]
If you want to access a member of the current class, the answers already given will work. But if the instance you are looking isn't part of the class, you are out of luck.
For example:
private function foo():void {
var box_2:Sprite;
trace(this["box_"+(i+1)]);
}
Won't work, because box_2 isn't a part of the class. In that case, it is highly recommended to use an array.
If you want to access a DisplayObject (for example, a Sprite or a MovieClip) you also can use getChildByName. But in that case, box_2 will be the name of the object, instead of the name of the variable. You set the name like
var box:Sprite;
box.name = "box_2";
But again, I recommend an array.
I have a class that uses a lot of database internally, so I built the constructor with a $db handle that I am supposed to pass to it.
I am just getting started with PHPUnit, and I am not sure how I should go ahead and pass the database handle through setup.
// Test code
public function setUp(/*do I pass a database handle through here, using a reference? aka &$db*/){
$this->_acl = new acl;
}
// Construct from acl class
public function __construct(Zend_Db_Adapter_Abstract $db, $config = array()){
You would do it like this:
public class TestMyACL extends PHPUnit_Framework_TestCase {
protected $adapter;
protected $config;
protected $myACL;
protected function setUp() {
$this->adapter = // however you create a new ZendDbADapter
$this->config = // however you create a new config array
$this->myACL = new ACL($this->adapter, $this->config); // This is the System Under Test (SUT)
}
}
IMHO, you need to work on your naming conventions. See Zend Framework Naming Conventions, for a start. An example would be the underscore, look up variables in the link. Also class naming.
You can do normally without reference same as constructor because this method is simplest.