ArrayCollection extend error - arrays

i extend ArrayCollection class for add push method
package com.cargo.collections
{
import mx.collections.ArrayCollection;
public class DataCollection extends ArrayCollection {
public function DataCollection(source:Array = null) {
super(source);
}
public function push(...parameters):uint {
var i:uint = source.push(parameters);
this.refresh();
return i;
}
}
}
but pushed data is array :/
var test:DataCollection = new DataCollection({id: 1});
test.source.push({id: 2});
test.push({id: 3});
output is
test = Array( {id: 1}, {id: 2}, Array({id: 3}) )

In your example ...parameters creates an array containing all the arguments passed to that function. This should work as expected:
public function push(...parameters):uint {
var i:uint = source.push(parameters[0]);
this.refresh();
return i;
}
Alternatively, if your purpose is to enable the pushing of multiple parameters you can use the Function.apply() method, which will translate a given array into multiple parameters:
public function push(...parameters):uint {
var i:uint = source.push.apply(null,parameters);
this.refresh();
return i;
}
This is the equivalent of saying
var i:uint = source.push(parameters[0],parameters[1],parameters[2]); // etc

Related

Missing properties in TypeScript

class RolloutStoreClass {
import { observable, action, makeAutoObservable } from "mobx";
public queue = observable<IDeploymentProject>([]);
public inProcess = observable<IDeploymentProject>([]);
public successfull = observable<IDeploymentProject>([]);
public failed = observable<IDeploymentProject>([]);
constructor() {
makeAutoObservable(this);
}
#action
private clearQueue(): void {
this.queue = [] ;
this.inProcess = [];
this.failed = [];
this.successfull = [];
}
}
export const RolloutStore = new RolloutStoreClass();
I get the issue on the clearQueue Function exactlly on this. queue
Error is:
In the "never []" type, the following properties of the "Observable Array " type are missing: "spliceWithArray, clear, replace, remove, toJSON".
You either need to make your queue (and other fields) regular array, all observability will still work.
Or use .clear() method inside clearQueue:
private clearQueue(): void {
this.queue.clear();
// ...
}
One more thing: when you use makeAutoObservable you don't need to explicitly mark action's observable's and so on, you can just drop all the decorators:
class Foo {
// Don't need explicit observable decorator
public queue: IDeploymentProject[] = [];
// ...
constructor() {
makeAutoObservable(this);
}
// You can remove action decorator
private clearQueue(): void {
this.queue = [] ;
this.inProcess = [];
this.failed = [];
this.successfull = [];
}
}

How to use Vector with offset from parent Haxe class instance?

What methods are available to achieve accessing parent instance Vector cell from another class?
My question is about the lines commented with "??":
class Test {
static function main() {
var a:A = new A();
var b:B = new B(a, 115);
}
}
class A {
public var cells:haxe.ds.Vector<Float>;
public function new() {
cells = new haxe.ds.Vector(1000);
}
}
class B {
public var a:A; // the "parent"
public var my_offset:Int;
public var my_cells:haxe.ds.Vector<Float>; //??
public function new(a:A, offset:Int) {
my_offset = offset;
my_cells[2] = 0.5; //?? actually a.cells[my_offset + 2];
}
}
Is it possible to:
access parent Vector memory directly?
use a macro?
use an abstract?
"Try Haxe" link
You could create an abstract type that wraps a Vector, automatically adding the desired offset by defining #:arrayAccess() methods:
class Main {
static function main() {
var cells = new haxe.ds.Vector(1000);
cells[115] = 1.5;
var cellsWithOffset = new VectorWithOffset(cells, 115);
trace(cellsWithOffset[0]); // 1.5
}
}
abstract VectorWithOffset<T>(VectorWithOffsetData<T>) {
public function new(vector:haxe.ds.Vector<T>, offset:Int) {
this = {vector: vector, offset: offset};
}
#:arrayAccess inline function get(i:Int):T {
return this.vector[i + this.offset];
}
#:arrayAccess inline function set(i:Int, v:T):T {
return this.vector[i + this.offset] = v;
}
}
typedef VectorWithOffsetData<T> = {
var vector:haxe.ds.Vector<T>;
var offset:Int;
}
Note: the abstract doesn't need to necessarily wrap a structure, it could also be a class. You can't wrap Vector directly though, as the offset needs to be stored somewhere, and abstracts can't have member variables of their own.

Invalid date in Chrome for typescript

I am new to TypeScript , please help for the problem below about invalid date in Chrome.
I have a ListViewModel and has code as :
module ListView {
export class ListViewModel{
public createdDate: Date;
public name: string;
constructor(listId:number) {
for(var i = 0; i < dataContext.List.length; i++) {
if(listId == dataContext.List.id){
// PROBLEM IS HERE:
this.createdDate = dataContext.Lists[i].createdDate;
this.name = dataContext.Lists[i].name;
}
}
}
}
}
and I have a controller which to get all list for my view, like
module ListController {
public aList : Array<ListViewModel>;
export class ListController(){
aList = [];
for(var i = 0; i < 2; i++) {
var newList = new ListViewModel(i);
aList.push(newList);
}
}
}
and dataContext is created manually in run.ts file , like :
Lists : [
new List(1, new Date(2014,1,2), "test1"),
new List(1, new Date(2014,06,07),"test2")
]
and I want to show the list in HTML and i have alread correctly set controller = ListController for this page , like :
<ul>
<li ng-repeat="n in aList">
<label>Name: {{n.name}}</label>
<label>Date: {{n.createdDate}}</label>
</li>
</ul>
The problem is : the list does not display , but in Chrome Console there is no any error.
When debug into ListModelView, this.createdDate = dataContext.Lists[i].createdDate; is an invalid date, but name is correctly set.
Thanks a lot for having a look.
I suspect the issue is as follows:
var date = new Date(2014,06,07);
Your literals 06 and 07 are octals, not decimal numbers.
To create a date, use decimals, like this:
var date = new Date(2014, 6, 7);
Browsers typically sort this out for you, but the TypeScript compiler will complain about this if you are targeting ECMAScript 5 or above.
If it isn't the compiler warning you are having trouble with, you can revisit your code to solve the simple syntactical issues that TypeScript should also be warning you about (it certainly warned me when I took your examples).
Here is a rudimentary running version, with some bits reverse engineered out of your question. I have given it a quick spin in Chrome and the date is logged correctly.
class List {
constructor(public id: number, public createdDate: Date, public name: string) {
}
}
var dataContext = {
Lists : [
new List(1, new Date(2014,1,2), "test1"),
new List(1, new Date(2014,6,7),"test2")
]
};
module ListView {
export class ListViewModel {
public createdDate: Date;
public name: string;
constructor(listId:number) {
for(var i = 0; i < dataContext.Lists.length; i++) {
console.log(dataContext.Lists[i].createdDate);
if(listId == dataContext.Lists[i].id) {
// PROBLEM IS HERE:
this.createdDate = dataContext.Lists[i].createdDate;
this.name = dataContext.Lists[i].name;
}
}
}
}
}
module ListController {
export var aList : Array<ListView.ListViewModel>;
export class ListController{
public aList = [];
constructor() {
for(var i = 0; i < 2; i++) {
var newList = new ListView.ListViewModel(i);
aList.push(newList);
}
}
}
}
var lv = new ListView.ListViewModel(1);
The snippet with the compiled JavaScript output is below (albeit I changed it to log to the visible page, rather than the console):
var List = (function () {
function List(id, createdDate, name) {
this.id = id;
this.createdDate = createdDate;
this.name = name;
}
return List;
})();
var dataContext = {
Lists: [
new List(1, new Date(2014, 1, 2), "test1"),
new List(1, new Date(2014, 06, 07), "test2")]
};
var ListView;
(function (ListView) {
var ListViewModel = (function () {
function ListViewModel(listId) {
for (var i = 0; i < dataContext.Lists.length; i++) {
document.getElementById('output').innerHTML += dataContext.Lists[i].createdDate + '<br />';
if (listId == dataContext.Lists[i].id) {
// PROBLEM IS HERE:
this.createdDate = dataContext.Lists[i].createdDate;
this.name = dataContext.Lists[i].name;
}
}
}
return ListViewModel;
})();
ListView.ListViewModel = ListViewModel;
})(ListView || (ListView = {}));
var ListController;
(function (_ListController) {
_ListController.aList;
var ListController = (function () {
function ListController() {
this.aList = [];
for (var i = 0; i < 2; i++) {
var newList = new ListView.ListViewModel(i);
_ListController.aList.push(newList);
}
}
return ListController;
})();
_ListController.ListController = ListController;
})(ListController || (ListController = {}));
var lv = new ListView.ListViewModel(1);
<div id="output"></div>

Finding objects index in a multidimensional array

Im making an inventory and Im adding stacks but ive hit an issue
below is what I want compared to what works
I just want to find the index of the object I pass through
myArray[0] = [item:object,StackAmmount:int]
var myArray:Array = new Array();
myArray[0] = ["name1",1];
myArray[1] = ["name2",1];
myArray[2] = ["name3",1];
trace("Name" , myArray[0][0]);
//traces "name1"
trace("Stack" , myArray[0][1]);
//traces "1"
trace("Index of Object" , myArray.indexOf("name2"));
//traces -1
// Not Working (NOT FOUND)
//How can I find the index of "item1" or "item2" in the above example
var myOtherArray:Array = new Array();
myOtherArray[0] = "name1";
myOtherArray[1] = "name2";
myOtherArray[2] = "name3";
trace("Name" , myOtherArray[0]);
//traces "name1"
trace("Index of Object" , myOtherArray.indexOf("name2"));
//traces 1
//Working
perhaps there is a better way of dealing with stacks?
Paste Bin Link: http://pastebin.com/CQZWFmST
I would use a custom class, therefore a 1D vector would be enough. The class would contain the name of the item, and the stack. You could subclass this class to override the maxStack variable of the item, and then the searches would be easier aswell, you could just iterate through the vector and check the name.
public class InventoryItem
{
protected var _name:String;
protected var _stack:int;
protected var _maxStack:int;
public function InventoryItem():void {
}
public function get name():String {
return _name;
}
public function get stack():int {
return _stack;
}
public function set stack(value:int):void {
_stack = value;
}
public function get maxStack():int {
return _maxStack;
}
}
...
public class InventoryWeapon extends InventoryItem
{
public function InventoryWeapon(__name:String, startStack:int) {
_maxStack = 64;
_name = __name;
_stack = startStack;
}
}

Adding movie clips into 2D array

package
{
import flash.events.Event;
import flash.display.MovieClip;
// class
public class GameGrid extends MovieClip
{
private var gameHeight:Number = 600;
private var gameWeight:Number = 800;
private var gridHeight:Number = 50;
private var gridWeight:Number = 50;
private var rowNumber:int = 12;
private var columnNumber:int = 16;
private var backgroundGrid:Array = new Array(12,16);
private var foregroundGrid:Array = new Array(12,16);
function GameGrid(){
}
function addBackGrid(rowN:int,colN:int,mcObject:MovieClip)
{
backgroundGrid[rowN,colN].push(mcObject);
}
function addForeGrid(rowN:int,colN:int,mcObject:MovieClip)
{
foregroundGrid[rowN,colN].push(mcObject);
}
function calculateRowDiff(rowA:int,rowB:int):Number
{
return Math.abs(rowA-rowB);
}
function calculateColDiff(colA:int,colB:int):Number
{
return Math.abs(colA-colB);
}
function calculateCorDiff(colA:int,colB:int,rowA:int,rowB:int):Number
{
return Math.sqrt((calculateRowDiff(rowA,rowB) * calculateRowDiff(rowA,rowB)) + (calculateColDiff(colA,colB) * calculateColDiff(colA,colB)));
}
// add to stage
function paintbackgroundGrid()
{
for (var i:int=0; i<16; i++)
{
for (var j:int=0; j<12; j++)
{
MovieClip(backgroundGrid[i,j]).x = i * 50;
MovieClip(backgroundGrid[i,j]).y = j * 50;
stage.addChild(MovieClip(backgroundGrid[i,j]));
}
}
}
}
}
So what this GameGrid class do is to hold an Array of grids(or tiles which extends MovieCLip) that will be added to the main stage and will call the initializeItem function.
function InitializeItem(e:Event)
{
var gamemap = new GameGrid();
var mc:MovieClip = new MainCharacter();
gamemap.addBackGrid(1,1,mc);
gamemap.paintbackgroundGrid();
//trace("Year: "+gameTime.gameYear+" Month: "+gameTime.gameMonth+" Day: "+gameTime.gameDay+" "+gameTime.gameHour+":"+gameTime.gameMinute+":"+gameTime.gameSecond);
}
The initializeItem should create an instance of gamegrid, and add movieclips to their respective locations(stored using array) and display them.
and this is the error stacktrace:
ReferenceError: Error #1069: Property 1 not found on Number and there is no default value.
at GameGrid/addBackGrid()
The debugger suggest that the error came from the line backgroundGrid[rowN,colN].push(mcObject);
Is there a way I can hold a 2d array movieclips? I'm new to AS3 and it looks very similar to JAVA, what am I missing?
Try this
private var backgroundGrid = [];
function addBackGrid(rowN:int,colN:int,mcObject:MovieClip) {
if (backgroundGrid[rowN] == null) {
backgroundGrid[rowN] = [];
}
backgroundGrid[rowN][colN] = mcObject;
}
In as3, following code means create a array and the array contains two elements, one is 12, and the other is 16.
private var backgroundGrid:Array = new Array(12,16);

Resources