How to replace content element in an Array Controller - arrays

Currently working on part of the app that requires cloning an element from the content, then modifying cloned element and saving back to model. I am having a problem when saving the cloned element and having to replace the old item with cloned one. What I am currently doing is changing all the properties of the old item like so (it works):
Blocks.replace = function(item1, item2) {
for(var k in item2) {
Ember.set(item1, k, item2[k]);
}
};
var selectedEmployment = this.get("controllers.employmentDataEntry").get("selectedEmployment");
var modelItem = content.findBy("#id", selectedEmployment["#id"]);
Blocks.replace(modelItem, selectedEmployment);
I'm trying to use the ArrayController replaceContent method, but I get an error saying "Invalid array length" when trying to run the following code:
var employmentIndex = content.indexOf(modelItem);
this.replaceContent(employmentIndex, 0, selectedEmployment);
Am I doing this incorrectly? Is there a better way of replacing an item?
Note: I am using JSON as a model. The ArrayController that is used when calling replaceContent contains an array of length 2.

Fix: Need to send in an array to replaceContent method. So change selectedEmployment to [SelectedEmployment]. Also, change 0 to 1, otherwise, content will end up having both element and cloned element.
this.replaceContent(employmentIndex, 1, [selectedEmployment]);

Related

Swift 3 - set the key when appending to array

I have this array where I set the keys on the creation. Now in some point in my view I load some more information based on ids (the keys).
var colors = [
"37027" : UIColor(red:150/255, green:57/255, blue:103/255, alpha:1),
"12183" : UIColor(red:234/255, green:234/255, blue:55/255, alpha:1),
"44146" : UIColor(red:244/255, green:204/255, blue:204/255, alpha:1)
]
I want to add more colors to this array dynamically. How can I insert new items in the array setting the key? Something like
colors["25252"] = UIColor(red:244/255, green:204/255, blue:204/255, alpha:1)
The line above doesn't work, it is just to illustrate what I need.
Thanks for any help
Update: the code above is an example. Below the real code:
var placedBeacons : [BeaconStruct] = []
BeaconModel.fetchBeaconsFromSqlite(completionHandler: {
beacons in
for item in beacons{
self.placedBeacons["\(item.major):\(item.minor)"] = item
}
})
Error: Cannot subscript a value of type '[BeaconStruct]' with an index of type String
To match the key subscripting
self.placedBeacons["\(item.major):\(item.minor)"] = item
you have to declare placedBeacons as dictionary rather than an array
var placedBeacons = [String:BeaconStruct]()
It requires that item is of type BeaconStruct
The code you wrote, it should work. I have used such kind of code and was able to implement successfully. I just tested your code in my end and it's working for me. I declared colors variable globally in my class file and in view did load method added the second code to add another item in my colors array. After printing it out. My output shows full list of array with 4 items and the number of array count return 4 as well.
Please let me know, more details of your scenario so i can help you to figure it out the issue. but looks like it should work.

How to add a selector to an existing rule?

Is there a way to add a selector to an existing rule with Postcss API?
Something like myRule.addSelector(".newSelector")?
Currently I have to do this:
myRule.selector = myRule.selector ? myRule.selector + ", .newSelector" : ".newSelector";
You can use rule.selectors, which is a property with a getter that returns an array of selectors (generated from the rule.selector string), and a setter that takes an array of selectors (and overwrites the rule.selector string).
var sels = myRule.selectors; //save the getter returned array so we can modify it with push.
sels.push('.newSelector'); //push returns the new length not the array, that's why we had to save the array in sels.
myRule.selectors = sels; //the setter will join the array elements and overwrite myRule.selector
A much shorter version using concat (because concat returns a new array).
myRule.selectors = myRule.selectors.concat('.newSelector');
See the PostCSS source here.

Add children from array

I've been trying to learn swift for a few days now, and all tutorials I've found had had a specified number of SKNodes. I'm trying to programmatically add new instances to an array nodeList and add them to the scene, because I want each of them to execute some code periodically. This is my current version of code for adding more objects:
if (last_created < 0) {
//Some other code here
for i in 0...3 {
self.nodeList.append(self.backObject);
var x_current = CGFloat(Float(arc4random())/4294967296.0 * Float(x_range) + Float(x_min));
var y_current = CGFloat(Float(y_min) - Float(y_range));
self.nodeList[self.nodeList.count-1].position = CGPoint(x: x_current, y: y_current);
self.addChild(self.nodeList[self.nodeList.count-1]);
}
}
Attempt to execute this raises an exception:
Attemped to add a SKNode which already has a parent
I assume that every item in my nodeList array is seen as the same object, rather than separate instances. However, I don't know how to fix the problem. How should I have done this?
Thanks for your help.
You are adding self.backObject to the array multiple times, so it is being added to the scene more than once, causing the error. You should create a new instance of the node within the loop and add that to the array.

Accessing instance properties inside of an array

I've imported several images into an actionScript 3 document. I've turned them all into symbols (movie clips) and given them instance names to reference from ActionScript.
Ok, so I'm putting the instances into an array so I can loop through them easily, but for some reason, whenever I'm putting in the instance name, I do a trace on the value in the array and it's giving me the symbol object back, rather than the instance object.
Basically trying to loop through the array to make each instance's visibility = false
Here's a sample:
var large_cap_extrusion_data: Array = new Array();
large_cap_extrusion_data[0] = large_cap_extrusion_menu_button;
large_cap_extrusion_data[1] = extrusion_border_large_cap
large_cap_extrusion_data[2] = "Large Cap";
large_cap_extrusion_data[3] = large_cap_main_menu_button;
var extrusion_data: Array = new Array();
extrusion_data[0] = large_cap_extrusion_data;
trace(extrusion_data[0][0]);
The traces gives:
[object large_cap_menu_button]
(the parent symbol)
rather than:
"large_cap_extrusion_menu_button"
I'd be very grateful if someone could tell me where I'm going wrong...
when you trace and object, by default it describes it type. What you want is the "name" property of the object.
Try this:
trace(extrusion_data[0][0].name);
that should give you the instance nema of the large_cap_menu_button rather than the class description. Either way, you have the right object I bet.

flex 3: Can anybody help me optimize this array -> arrayCollection function?

I'm using a parent to pass a multi-dimensional array to a child. Structure of the array, named projectPositions is as follows (with example data):
projectPositions[0][0] = 1;
projectPositions[0][1] = 5;
projectPositions[0][2] = '1AD';
projectPositions[0][3] = 'User name';
I need to take this inherited array and turn it into an arrayCollection so that I can use it as a dataProvider. Currently, my init function (which runs onCreationComplete) has this code in it to handle this task of array -> arrayCollection:
for (var i:int = 0; i < projectPositions.length; i++)
{
tempObject = new Object;
tempObject.startOffset = projectPositions[i][0];
tempObject.numDays = projectPositions[i][1];
tempObject.role = projectPositions[i][2];
tempObject.student = projectPositions[i][3];
positionsAC.addItemAt(tempObject, positionsAC.length);
}
Then, during a repeater, I use positionsAC as the dataprovider and reference the items in the following way:
<mx:Repeater id="indPositions" dataProvider="{positionsAC}" startingIndex="0" count="{projectPositions.length}">
<components:block id="thisBlock" offSet="{indPositions.currentItem.startOffset}" numDays="{indPositions.currentItem.numDays}" position="{indPositions.currentItem.role}" sName="{indPositions.currentItem.student}" />
</mx:Repeater>
This all works fine and returns the desired effect, but the load time of this application is around 10 seconds. I'm 99% sure that the load time is caused by the array -> arrayCollection for loop. Is there an easier way to achieve the desired effect without having to wait so long for the page to load?
The issue your having loading items could be because you are using a repeater instead of a list class.
With a repeater, there will be a block created in memory, and drawn on the screen. So, if you have 100 items in your array, then 100 blocks will be created. this could slow down both initial creation and the overall app.
A list based class focuses on a technique called renderer recycling; which means only the displayed elements are created and rendered on the screen. So, depending on settings, you'd usually have 7-10 'block' instances on the screen, no matter how many items you have in your array.
change
positionsAC.addItemAt(tempObject, positionsAC.length);
to
positionsAC.addItem(tempObject);
addItemAt is causing a reindex of the collection which can greatly slow down the collection.
[EDIT]
Put this trace statement before and after the loop
take the output and subtract one from the other and that will show how many milliseconds the loop has run.
var date:Date = new Date( );
trace( date.getTime())

Resources