ATK4: Error: Using $this when not in object context - atk4

I am defining a method in ATK4 with an expression field in it:
$this->addExpression('answered')->set(function($model,$select){
return $select->dsql()
->table('answer')
->field($select->expr('if(count(*)>0,"Y","") as answered'))
->where('usecollection_id',$select->getField('id'))
->where('student_id',$this->api->auth->get('id'));
})->type('boolean')->caption('Besvaret');
It works fine on my development machine, but on the production server it throws a fatal error: PHP Fatal error: Using $this when not in object context in file.php
This is the problematic line:
$this->api->auth->get('id')
Any idea what causes this difference and how to make it work?

One way is to pass $this to enclosed function like this:
$self = $this;
$this->addExpression('answered')->set(function($model,$select)use($self){
echo $self->api->auth->get('id');
});
That's how you should pass variables quite often to anonymous functions in PHP.
Another possibility in this particular case is to simply use $select->api->..., $model->api->... or any other ATK4 object instead of this->api->.... That's because all ATK4 objects have reference to API class, so $whateverObject->api->... will always work and always point to same API class object.

You're referencing a variable ($this) that is not passed nor declared within the function (you're passing $model and $select to the function).
I'm not smart enough to tell you the best way to fix this, maybe you can define $auth_id (set it to $this->api->auth->get('id')) before the function is called and pass that variable to it as well?
There is bound to be a better way though...

Related

How to access a main document class array from a movieclip?

I have an array in my main
public var graphArray:Array = [1,2,3,4,5,6];
And I'm trying to access it from within a MovieClip that I've put on my timeline using:
var graph1scale:Number = MovieClip(root).graphArray[0]
It looks like it would make sense to me but when I try to run it I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Am I wrong to be using MovieClip(root) to try and access it? I've only just started using external classes (this is my first project doing so) and usually I just do everything on the timeline. So MovieClip(root) is familiar to me but I guess it's not the right thing to do here.
Is there a way I can access vars from Main.as?
-----SOLVED-----
I realised MovieClip(root) did work all along but I was just calling on the array before the array was being defined in Main.as. I put a delay on calling graphArray and it worked.
Not sure how that makes sense though because the graphArray is the first thing I've defined in the whole main.as class
Try using this instead
MovieClip(this.root)
This works for me on a test that you can see here:
http://marksost.com/test/as3arrayaccess/
And the source files here:
http://marksost.com/test/as3arrayaccess/test.zip

ironjs: returning objects to JS

Using Ironjs. I have a c# function registered as a JS function (via SetGlobal)
It gets called , but I want to return a value to from that function. The value is an IEnumerable of CLR objects. Using Jint this just works: I return the object and can foreach it etc, how do I do the same thing in IronJS (Why not use Jint, well it has bugs, for example it wont compile underscore.js)
EDIT: Since I am not a fluent F# person I cannot answer this question myself by reading the code. So instead I fixed Jint. However it would still be nice to know the answer
We are still working on our .NET interop. As such, the foreach in IronJS is not set up to enumerate IEnumerables, but instead works on CommonObject type objects.

extjs function declaration syntax

In extjs, we often have syntax like this:
someFunction = function(){}
or:
someFunction : function(){}
What is the difference between the two? Also, what enables exts to use this syntax as opposed to the normal javascript syntax?
So far as i know, javascript syntax is like this:
function(){}//No '=' or ':'
There is not ExtJS function syntax. All these methods of defining a function are part of JavaScript and there is nothing new introduced by ExtJS. Lets take each case
function functionname() -
This is most common and is coming from the procedural programming school. Basically you are writing global functions and these are called by other functions in your script
Enter OOP in Javascript.. there is where the next two methods come in! Javascript is very flexible and extensible. Functions can be stored in variables, passed into other
functions as arguments, passed out of functions as return values, and constructed at run-time. You can also have anonymous functions! coming back...
someFunction = function() - In this case, you are storing a function in the variable 'comeFunction'.This variable can be part of an object or separate (But internally everything in javascript is object except for primitive data types).
someFunction : function() - In this case also, you are storing the function in the variable but this is during object declaration. You will see them used in ExtJS because it follows OOP.
You could also inject a method or modify the method you already specified by the above two methods. I hope this helps you understand more about functions.

Call Silverlight methods from Javascript by name

Is it possible to get a reference to a Silverlight method purely by name from Javascript, and then invoke it? With pure Javascript objects you would be something like this:
var f = theObj["theMethodName"];
f.call(theObj, "an arg");
But treating a Silverlight object as an associative array doesn't seem work.
I'm guessing I could probably use Eval as a last resort, but I'd rather avoid it.
The question is on how to call a Silverlight function from Javascript by name. You can easily call methods on an object directly by enabling a method for scripting using the ScriptableMember attribute, but you can't invoke it as a string directly.
I think you're stuck with eval.
HtmlPage.Window.Invoke("theMethodName", "An arg");
OR
var obj = HtmlPage.Document.GetElementByID("theObj");
obj.Invoke("theMethodName", "an Arg");
...
Ah, re-reading it...no, no access to the reflection API. You'd have to expose it formally. Its still a managed object...just exposed as an 'object' in JScript. So not the same as a prototype object.
This works:
theObj["theMethodName"]("an arg");
But this does not:
theObj["theMethodName"].apply(null, "an arg");
at least I didn't manage to use apply (and call) :(

Ruby C Extension using Singleton

I only wanted to allow one instance of my C extension class to be made, so I wanted to include the singleton module.
void Init_mousetest() {
VALUE mouseclass = rb_define_class("MyMouse",rb_cObject);
rb_require("singleton");
VALUE singletonmodule = rb_const_get(rb_cObject,rb_intern("Singleton"));
rb_include_module(mouseclass,singletonmodule);
rb_funcall(singletonmodule,rb_intern("included"),1,mouseclass);
### ^ Why do I need this line here?
rb_define_method(mouseclass,"run",method_run,0);
rb_define_method(mouseclass,"spawn",method_spawn,0);
rb_define_method(mouseclass,"stop",method_stop,0);
}
As I understand it, what that line does is the same as Singleton.included(MyMouse), but if I try to invoke that, I get
irb(main):006:0> Singleton.included(MyMouse)
NoMethodError: private method `included' called for Singleton:Module
from (irb):6
from C:/Ruby19/bin/irb:12:in `<main>'
Why does rb_include_module behave differently than I would expect it to? Also any tangential discussions/explanations or related articles are appreciated. Ruby beginner here.
Also it seems like I could have just kept my extension as simple as possible and just hack some kind of interface later on to ensure I only allow one instance. Or just put my mouse related methods into a module... Any of that make sense?
according to http://www.groupsrv.com/computers/about105620.html the rb_include_module() is actually just Module#append_features.
Apparently Module#include calls Module#append_features and Module#included. So in our C code we must also call included. Since clearly something important happens there.

Resources