Categories
discuss

should I pass function parameters encapsuled in an object or one by one if performance matters in Javascript?

I’m often finding myself passing around a lot of parameters from function to function. Looks like this:

ajaxLiveUpdate : function (bindTo, func, interval, 
                      dyn, el, lib_template, locale, what) {
   // do sth
}

While I could see that storing these in an object specs would make sense “visually” I’m curious as to the performance implications of creating the object, assinging all the key/val pairs and passing the object around.

Question:
If every ms counts, what is the best and most structured way to shuffle around a large number of function parameters?

Thanks!

Answer

I think a good optimizer would make this micro-optimization insignificant. Feel free to use whichever suits your coding style and code reading habits better. If you prefer an object, because it’s more compact, go ahead and use an object. If you’d rather spell out arguments expressly, then do that.

Categories
discuss

How to utilize generic AJAX call with multiple success functions

I am making an ajax call that returns XML. This XML needs to be handled differently based upon the section of the page within the site the user is on. Thus, I would like to implement 1 ajax function that makes the calls, and has a variable success function… I’m sure it is simple but I’ve searched for a while and cannot figure it out..

function makeAjaxCall(variableSuccessFunction) {
    $.ajax.... (ajax stuff goes here)...
    success: variableSuccessFunction(xml)
}
function ViewOne(xml) {
    //take the XML and update the dom as appropriate
}
function ViewTwo(xml) {
    //take the XML and update the dom as appropriate
}

$(document).ready(function() {
    //be able to call either one of these functions
    makeAjaxCall(ViewOne);
    makeAjaxCall(ViewTwo);

}

Answer

You’ve basically got it! Just one tweak:

function makeAjaxCall(variableSuccessFunction) {
    $.ajax.... (ajax stuff goes here)...
    success: variableSuccessFunction // no (xml)
}

You’re passing around function references. success is passed a reference to variableSuccessFunction (whatever that may be) and will call it just like it would if you had supplied an anonymous function to it. No need to invoke it inside of makeAjaxCall.

Categories
discuss

Detecting when a user drags over an adcontrol in a HTML5/Javascript app?

Hi I developed a game using Construct2 and included an AdControl inside main html page that came with the exported project. Now I noticed when you drag from the game canvas onto the ad and release, it never actually releases the touch. My game is dependent on a particular touch so I actually need that touch to reset if the user stops touching the screen. Am I missing something here, or is there maybe a workaround for this issue? I tried out the same thing in C#, using a WebBrowser control and I didn’t experience the same issue.

I have tried a few things like HTML fragments and separating the div from the canvas with margins but it always acts the same way. I also tried to include the ad using a Windows Runtime Component but it doesn’t look like it allows you to pass UI elements like the AdControl. Does anyone have some suggestions. Thanks.

Answer

Here is a working example which releases the touch when the hand is moved over the ad : http://jsfiddle.net/ChaitanyaMunipalle/jn3kZ/

You can see all the touch events below the canvas in this jsfiddle.

Check HammerJs for drag, release and other touch gestures. You can use touch events like normal jquery events like

$('#canvasId').on('dragstart drag dragend release ....',callback);
Categories
discuss

HTML5 SVG vs Canvas for big number of lines?

Question:
Is canvas more suitable than svg in the following case?

Case:
I’m drawing a chart (using d3js library) similar to this one (but with much more data):

http://mbostock.github.com/d3/talk/20111116/iris-parallel.html

It’s based on an svg and it works fine for several thousands of lines (up to 5000), adding more lines (svg path) decreases the performance dramatically (scrolling in the page becomes slow)

Keep in mind: That I need to add mouse events (which is handy in svg)

Answer

Generally svg is better suited for vector images, like in your example. However canvas has a lot of benefits in modern browsers such as hardware acceleration, so for drawing the lines, as long as zooming, panning ect. isn’t required performance will be using canvas.

Mouse events can be a pain using canvas, since you have to manually keep track of everything, so with 5000+ points using canvas it wont be fun. The trade off however will be once the points are drawn, assuming you only draw them once the page will behave fine regardless of the number of lines, since they are all drawn to a raster image and aren’t part of the DOM.

Honestly though the best way to find it is to test what you currently have using canvas.

Categories
discuss

Mongoosejs refresh a document

Suppose I have a document for example: var doc = Model.findOne({name:”name”});

Now if the document gets edited trough another connection the the database, doc doesn’t hold the right information. I do need it, so I have to “refresh” or “redownload” it from the database. Is there any way to do this with only the object “doc”?

Answer

Assuming doc contains the document instance to refresh, you can do this to generically refresh it:

doc.model(doc.constructor.modelName).findOne({_id: doc._id},
    function(err, newDoc) {
        if (!err) {
            doc = newDoc;
        }
    }
);

However, it’s better to not persist/cache Mongoose document instances beyond your immediate need for them. Cache the immutable _id of docs you need to quickly access, not the docs themselves.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..