I have overcome a problem, but my solution seems really inefficient and clunky. Please consider this problem:
I have an array collection with articles in.
This array collection is filtered to a currently selected article category.
There is a cursor bound to a view that shows the current article.
When a category is deleted I need to delete orphened articles, I can't use the cursor or collection without affecting the views as they are bound.
I can itterate over the source, but if I delete (splice) from the source I have to break and start again, as all the articles indexes change, even when using for each.
This seems like a really inefficient way of doing things, and I'm sure there is a better way to do it, eithe by using another itterator on the same source, or unbinding the views unill I have finished updating, etc
Please let me know if I am missing a trick as I'm sure this is a really common problem/issue.
Thanks
Rob
p.s. Wrote this on my iPhone. Sorry for any mistakes!
Run the loop backwards.
So, instead of, say:
var len:int = arr.length;
for(var i:int = 0; i < len; i++) {
if(some condition) {
arr.splice(i,1);
}
}
do this:
for(var i:int = arr.length - 1; i >= 0; i--) {
if(some condition) {
arr.splice(i,1);
}
}
Simplest solution would probably be to just save the indexes you need to remove in a temporary array. Then after you've iterated through the collection, go back and just remove the items in the temporary array.
But from what I can gather, you should probably use a hash (Object) or something instead of an array structure.
Related
I need to create a css stylesheet for an existhng html page. Is there any easy way to extract all the classes and ids using a script... I am familiar with python, java, c, javascript and php. Is there any library for those languages? re sems a bit overkill for me. grateful for any help.
cheers!
The easiest way to retrieve the ids of each of the elements is to simply iterate over the elements that have an id, and retrieve that:
var idEls = document.querySelectorAll('*[id]'),
ids = [];
for (var i=0, len=idEls.length; i<len; i++){
ids.push(idEls[i].id);
}
One way to cover those browsers that don't implement querySelectorAll() is to create a nodeList of every element, then iterate over that (quite probably very large) nodeList, test for the id and, if it exists, push that into the array:
var els = document.getElementsByTagName('*'),
ids = [];
for (var i=0, len=els.length; i<len; i++){
if (els[i].id){
ids.push(els[i].id);
}
}
Using jquery, loop through the elements and get id and class
$('html *').each(function(){
console.log($(this).attr('id'), $(this).attr('class'))
})
Is it possible to programatically sort properties on DocumentType? I am creating them from code, but not sure how to order them.
Any suggestions much appreciated.
It depends on how and what you want to sort, but here's an example of how you might sort them:
DocumentType dt = DocumentType.GetByAlias("umbTextpage");
//Get the one you want to move to the top.
var property = dt.PropertyTypes.First(p => p.Alias == "bodyText");
//Get the rest. Make sure you have the right TabId.
var otherProperties = dt.PropertyTypes.Where(p => p.Alias != "bodyText" && p.TabId == 8).ToList();
property.SortOrder = 0;
property.Save();
int i = 1;
foreach (var p in otherProperties)
{
p.SortOrder = i++;
p.Save();
}
Hopefully, that gives you some sort of starting place, if you haven't already figured it our already... The easiest way to get the TabId is to look at the cmsTab table in the database.
Do you refer sorting based of the sub-pages of particular node? I mean your question little confuse...
anyway if you want to sort sub-pages; you can do that by something like following.
var eventsNode = #Model.NodeById(1058).orderbydesending("createddate");
Thanks,
Developerjigar
i am trying to figure out if i can in anny way get the exact position for every character inside a textflow?, also I'm having trouble with the TLF documentation, is there anny documentation that shows more on how to work with it in actionscript rather than mxml, im looking to write my own component and perhaps not use richtexteditor if i don't need to.
Many thanks!
Edit: i finally think i figured out how to get each characters position in the textflow:
private function getCharPosition():void {
for (var i:int=0; i<=textController.flowComposer.numLines; i++) {
var textFlowLine:TextFlowLine = textController.flowComposer.findLineAtPosition(i);
var textLine:TextLine = textFlowLine.getTextLine();
trace('number of atoms in this line: ' + textline.atomCount);
for (var j:int=0; j<=textLine.atomCount; j++) {
try {
trace(textLine.getAtomBounds(j));
} catch (e:Error) {
trace('error');
}
}
}
}
This returns an error that's why i have try and cache, i have tried to change textLine.atomCount to -1 but that wont work either. Now i don't know what character exactly that i have the position for. Lots of figuring out yet...
You can use some of these steps which (among other things) allow to determine character's bounds.
What about your second question you can refer to the following documentation and/or some samples.
I have some values stores in my model. I need to create a copy of those values, make some changes, and then output those changes without affecting the model values.
var my_source:Array = model.something.source
var output:Array = new Array();
for each (var vo:my_vo in my_source) {
if (vo.id == 1) {
vo.name = 'Foo';
output.push(vo);
}
else if (vo.id == 21) {
vo.name = 'Bar';
output.push(vo);
}
}
return output;
So, this works fine, except that any changes that are made when looping through my_source also seems to affect model.something. Why do changes to the my_source array affect the model? How do I prevent this from happening?
I've mentioned how to do this in my blog, but short answer is use ObjectUtil.copy(). What you're trying to do isn't copying since Flash uses reference based objects, so you're only copying the reference to the other array. By using ObjectUtil.copy(), you're doing what's called a 'deep copy' which is actually recreates the object in a new memory location.
You are dealing with references to data, not copies of data. This is how ActionScript-3 (and many other languages) works.
When you create the my_source variable, you are creating a reference to model.something.source, which also includes all of the references to your model objects. Further, when you loop through the my_vo objects, you are also getting a reference to these objects. This means that if you make changes to the object in this loop, you are making changes to the objects in the model.
How do you fix this? Inside your loop, you will need to make a copy of your object. I don't know what my_vo looks like, but if you have any other objects in that object tree, they would be references as well, which would probably require a "deep copy" to achieve what you want.
The easiest way (but usually not the most efficient way) to achieve a "deep copy" is to serialize and de-serialze. One way to achieve this:
function deepCopy(source:Object):* {
var serializer:ByteArray = new ByteArray();
serializer.writeObject(source);
serializer.position = 0;
return serializer.readObject();
}
Then, in your loop, you can make your copy of the data:
for each(var vo:my_vo in my_source) {
var copy:my_vo = deepCopy(vo);
// act on copy instead of vo
}
One of the functionalities in my current flex application requires me to maintain a comments section. Here, the users can post the comments and the replies to existing comments. All I want is like the usual thread style commenting.
Let say, I am replying to someone else's comment, so, it will align my comment by a tab or so and people can easily see the comments and replies.
e.g.
USER ABC : COMMENT 1
-----USER XYZ: RE:COMMENT1
----------USER DEF: RE:RE:COMMENT1
and so on...
Can anyone suggest a way to do this?
Thanks :)
In your database you would store the parentID for each comment. You can query that data and loop through it creating new objects to display the comments however you want.
Store the results in an ArrayCollection
var tmpCanvas:Canvas;
for(var i:int = 0; i < ac.length; i++)
{
// display data
tmpCanvas = new Canvas();
tmpCanvas.x = ...
tmpCanvas.y = ...
// add items to the canvas
this.addChild(tmpCanvas);
}