calling private function inside knockout viewmodel - html

I have this viewmodel
var MyVM = function() {
var self = this;
// i wrote private function like this
function getNames() {
//some logic
}
//i want to call function inside this code
$("body").on("onTrigger", (evt, msg) => {
getNames();
});
}
but it doesnt call the function getNames(). How to call private function?

try to make changes in your code like this,
var names = {
getNames : function(){
//some logic
}}
then call the function this way:
names.getNames();
or create an object of MyVM and call like this:
MyVM names = new MyVM();
names.getNames();

Related

Action Script Convert Function to an Object

I need to convert a function to an object. For example, when I need to use the variable called fn I want to be able to use it as a function fn() or as an object fn.json(). I have code to do it, but I think it's not correct.
package lib.smartic {
// import
import lib.smartic.smartic;
// constructor $
public var fn = function(s):smartic{
return new smartic(s);
};
Function.prototype.json = function (s) {
// call
};}
How can I apply the prototype to my variable fn, not just to the object class?
Actually, Function is an Object... and as said #The_asMan, you shouldn't use prototypes.
Simple example:
var smartic: Smartic = new Smartic("someValue");
trace(smartic.json());
And definition of your Smartic class, without prototypes:
public class Smartic {
private var _value:String;
public function Smartic(value:String) {
_value = value;
}
public function json():String {
return "Some json here";
}
}
I don't know what you want to do exactily but if you want smartic to be a function you can do something like this:
keep the code givent by Nicolas Siver and add this function:
public function smarticFunc(value:String):Smartic
{
return new Smartic(value);
}
so you can use smarticFunc as function or as Smartic as it returns a Smartic.

Using addeventlistener handle another class file

I know I can use the addEventListener method to handle one:
addEventListener(SFSEvent.CONNECTION, MyMethod)
as I would for handling a method in another class? Like...
addEventListener(SFSEvent.CONNECTION, Myclass.class)
or
addEventListener(SFSEvent.CONNECTION, MyClass.method)
You may pass another function handler to a class
For example
Class A {
public function A() {
addEventListener(SFSEvent.CONNECTION, MyMethod);
}
private function _handler:Function;
public function set handler(value:Function):void {
_handler = value;
}
private function MyMethod(e:SFSEvent):void {
if (_handler) {
_handler.apply(null, someParam);
}
}
}
Then pass the target handler to A instance
var a:A = new A();
var b:Myclass = new Myclass();
a.handler = b.someMethod;
If the function is a static function, You may just do it like this
addEventListener(SFSEvent.CONNECTION, SomeClass.aStaticFunction);

AS3. Return a value with Loader Complete Event

I want to make some function, with returning loading object.
Something like:
var myVar:String;
myVar = MyFuncs.GetResponse("http://www....");
And GetResponse function must return some string value, for example json-like text.
I try.. But cant understend.
public function GetResponse(url:String):String{
var request:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.load(request);
return loader.data
}
But data is not loaded yet, when I return the value.
I understand, I need to add listener, when loader is complete:
loader.addEventListener(Event.COMPLETE, Complete);
But cant understand, how can I return the loaded value, when the loading is complete.
Because it will be a another function..
Please, help, if someone know, how :)
Sorry for my English, please.
You can create a custom loader, and set a callback function to it, when the loader load complete, the callback will be executed. Here is a simple example
public class MyLoader extends Loader
{
public function MyLoader($callBack:Function = null)
{
super();
callBack = $callBack;
this.contentLoaderInfo.addEventListener(Event.COMPLETE, Complete);
}
private var callBack:Function;
private var _url:String;
public function set url(value:String):void {
if (_url != value) {
_url = value;
var request:URLRequest = new URLRequest(_url);
this.load(request);
}
}
protected function Complete(event:Event):void {
var target:Object = event.target;
if (callBack) {
callBack.apply(null, [target]);
}
}
And you can use it like this in class A
public function class A {
public function test():void {
var loader:MyLoader = new MyLoader(setData);
loader.url = "assets/pig.jpg";//you asset url
}
private function setData(obj:Object):void {
//the obj type is LoadInfo
}
}

Flash cs5: Set a variable using a function, then read that variable from a differect function

I have a public variable and I am trying to set it, then read it from a different function:
public var str:String;
public function DailyVerse()
{
function create() {
str = "hello";
}
function take() {
var message:String = str;
trace(message);
}
take();
}
My trace results says null. Why does it not give me "hello"?
I'm not sure why you have this set up this way.... if you want to get and set variable, you use the getter and setter syntax for flash.
private var myRestrictedString:String;
public function get DailyVerse():String {
if(myRestrictedString == undefined) {
//Not yet created
myRestrictedString = "Something";
}
return myRestrictedString;
}
public function set DaileyVerse(string:String):void {
myRestrictedString = string;
}
Now you can access this from outside of your class like so:
myClass.DailyVerse = "Test";
trace(myClass.DailyVerse); //Outputs "Test"

Passing a function to another function in Actionscript 3

I have a function that passes an array to another function as an argument, there will be multiple data types in this array but I want to know how to pass a function or a reference to a function so the other function can call it at any time.
ex.
function A:
add(new Array("hello", some function));
function B:
public function b(args:Array) {
var myString = args[0];
var myFunc = args[1];
}
Simply pass the function name as an argument, no, just like in AS2 or JavaScript?
function functionToPass()
{
}
function otherFunction( f:Function )
{
// passed-in function available here
f();
}
otherFunction( functionToPass );
This is very easy in ActionScript:
function someFunction(foo, bar) {
...
}
function a() {
b(["hello", someFunction]);
}
function b(args:Array) {
var myFunc:Function = args[1];
myFunc(123, "helloworld");
}
You can do the following:
add(["string", function():void
{
trace('Code...');
}]);
...or...
...
add(["string", someFunction]);
...
private function someFunction():void
{
trace('Code...');
}