Actionscript - Dynamically assigning imported objects - object

Take this simplified code
import assets.panels.About1;
import assets.panels.About2;
import assets.panels.About3;
private var _panel:*;
_panel = new About1();
It is possible to define About1 as a variable, so I can set something like
var aboutPanel = 'About3';
So code executed would be
_panel = new About3();

To do that you can use flash.utils.getDefinitionByName like this :
var class_name:String = 'MyClass';
var my_class:Class = flash.utils.getDefinitionByName(class_name) as Class;
trace(my_class); // gives : [class MyClass]
var obj = new my_class();
addChild(obj);
trace(obj); // gives : [object MyClass]
Hope that can help.

Related

Revert variables inside a Loop in Node JS

I am trying one logic in Node JS code which looks something like below:-
var startPersons1 = JSON.parse(JSON.stringify(persons1));
var startPersons2 = JSON.parse(JSON.stringify(persons2));
var startPersons3 = JSON.parse(JSON.stringify(persons3));
var startPersons4 = JSON.parse(JSON.stringify(persons4));
.....
// Operations on persons1, persons2, persons3, persons4 which are changed in methods called here
....
// Now persons1, persons2, persons3, persons4 are modified
// Now, if I wanted persons1, persons2, persons3, persons4 to come to their original state above, ie.
// startPersons1, startPersons2, startPersons3, startPersons4, I am doing something like this
persons1 = JSON.parse(JSON.stringify(startPersons1));
persons2 = JSON.parse(JSON.stringify(startPersons2));
persons3 = JSON.parse(JSON.stringify(startPersons3));
persons4 = JSON.parse(JSON.stringify(startPersons4));
You can assume this is inside some for loop.
So, is there a better way to do this revert everytime. The number of variables can increase by lot.
if persons1, persons2 etc are inside an array, you could use map function
var persons = [/*array of persons*/]
var newpersons = persons.map(function(person) {
/*change here your person object and return it.*/
return person
})
/*here persons is the originalone, newpersone is the new version*/

Export class and instanciate it immediately on import

I have a ES6 class in NodeJS 4 :
vehicule.js
"use strict";
class Vehicule {
constructor(color) {
this.color = color;
}
}
module.exports = Vehicule;
When I need to instanciate in another file, I find myself doing this :
var _Vehicule = require("./vehicule.js");
var Vehicule = new _Vehicule();
I'm new to nodeJs, is there a way to do this in one line, or at least in a more readable way ?
A class is really supposed to be reused for many objects. So you should require the class itself:
var Vehicule = require("./vehicule.js");
and then create objects from it:
var vehicle1 = new Vehicule('some data here');
var vehicle2 = new Vehicule('other data here');
Usually classes start with an upper case letter, and instances of classes (objects themselves) with a lower case one.
If you want a "class" for just one objects, you can just create an inline object:
var vehicle = {
myProperty: 'something'
};
module.exports = vehicle;
//in some other file
var vehicle = require('vehicle');
Though if you really, really want to do that in one line, you can do:
var vehicle = new (require('vehicle'))('some constructor data here');
But that is not recommended. Pretty much ever.
If you really want to do this in one line:
var Vehicule = new (require('./vehicule'))('red');
but personally i would prefer two lines for the same reasons mentioned by #ralh.

Creating dynamic customer group using suite script

I am trying to create dynamic customer group using suite script in Net suite, I am trying below code but always getting
system INVALID_KEY_OR_REF
Invalid savedsearch reference key 21.
I have checked it is valid save search, Please help I am doing something wrong.
function createDynamicGroup(savedSearchId, groupName) {
var saveSearchObj = nlapiLoadSearch('customer', savedSearchId);
var initValues = new Array();
initValues.grouptype = 'Customer';
initValues.dynamic = 'T';
var goupRecObj = nlapiCreateRecord('entitygroup', initValues);
goupRecObj.setFieldValue('groupname', groupName);
goupRecObj.setFieldValue('savedsearch',saveSearchObj.getId());
nlapiSubmitRecord(goupRecObj);
}
You need group type = 'CustJob' as well as using a public search id:
function createDynamicGroup(savedSearchId, groupName) {
var saveSearchObj = nlapiLoadSearch('customer', savedSearchId);
var initValues = {
grouptype: 'CustJob', // <-- use this
dynamic: 'T'
};
var goupRecObj = nlapiCreateRecord('entitygroup', initValues);
goupRecObj.setFieldValue('groupname', groupName);
goupRecObj.setFieldValue('savedsearch', savedSearchId);
return nlapiSubmitRecord(goupRecObj);
}

Get the object id when the object is created (Parse.com)

I would like to set the string value to the object id when I create the object below. At the moment it prints "nil". Any ideas? Thanks
var date:NSDate = currentDatePicker.date
var DOB:NSDate = DOBPicker.date
var user = PFUser.currentUser()
var report = PFObject(className:"reports")
report["name"] = nameTextField.text
report["DOB"] = DOB
report["date"] = date
report["user"] = user
report.saveInBackground()
var ojId = report.objectId
println(ojId)
saveInBackground is an asynchronous method, so it doesn't wait. The reason why the id is not set is because the entity has not been saved yet.
I think you should use saveInBackgroundWithBlock: and retrieve the object id form within the block (which is executed after the entity has been saved):
var report = PFObject(className:"reports")
report["name"] = nameTextField.text
report["DOB"] = DOB
report["date"] = date
report["user"] = user
report.saveInBackgroundWithBlock { (success, error) -> Void in
if success {
var ojId = report.objectId
println(ojId)
}
}
or maybe also save, which is performed synchronously - but do not use that from the main thread:
report.save()
var ojId = report.objectId
println(ojId)

var v1 = new class(params) , object = {}

Hi I don't know what exactly do this kind of sentences:
var v1 = new class(params) , object = {}
The real example was: (From https://github.com/visionmedia/parted Usage paragraph)
var parser = new multipart(type, options) , parts = {};
I understand that parser will be a new multipart object, but parts?! what exactly do? Create empty object? where I have to push some data?
Thank's in advice!
var declarations can take multiple variables. Example:
var a = 1, b = 2;
That declares two variables, a and b, and assigns them the values 1 and 2, respectively.
In other words, in your example, parts is a new variable that is assigned an "empty" object.
{} in javascript creates a new empty object using JavaScript's literal object notation.
Other examples of creating objects using the literal object notation:
obj1 = { foo: 2 } // Now obj1.foo is 2
obj2 = { foo: 3, bar: "hello" } // Now obj2.foo is 3 and obj2.bar is "hello"

Resources