When to consolidate methods - migrate [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Below are my 7 call back methods for ajax. They are all similar in structure so that if I wanted I could abstract out the code into generic methods, call these generic methods, and modify them with input parameters/arguments.
However, I'm trying to determine the point at which consolidation is worth the effort. Furthermore, what is considered best practice?
Shold I consolidate these methods?
/*
consolidate these ajax call back functions
*/
function ajax_signup(a,b)
{
c=check_aml(a.slice(0,6));
if(c===0)
{
document.f1_1.submit(); // on pass submit the file upload form
}
else if(c===1)
{
document.getElementById(b).innerHTML=a; // on fail report to user
}
else if(c===2)
{
a=a.substr(6);
alert('php error: ' + a); // on php error
}
}
function ajax_signin(a,b)
{
c=check_aml(a.slice(0,6));
if(c===0)
{
m6(); // on pass reload the page
}
else if(c===1)
{
document.getElementById(b).innerHTML=''; // on fail report to the user
document.getElementById(b).innerHTML=a;
}
else if(c===2)
{
a=a.substr(6);
alert('php error: ' + a); // on php error
}
}
/*
Takes in sturctured data and convert to xhtml - remove the conversion into another method
*/
function ajax_tweet(a,b)
{
var c,d,e,f='';
var g=a.slice(0,6); // prepare status
var h=check_aml(g); // check for status
if(h===0)
{
c=a.split(/\|\|/);
for(e=0;e<c.length;e++)
{
d=c[e].split(/\|/);
f=f+'<div class="Bb2b"><img class="a" src="pictures/' + d[0] + '.jpg" alt=""/><a class="a" href="javascript:void(0)\">' + d[1] + ' posted ' + view_date(d[2],d[3]) + '</a><br/><p class="c">' + d[4] + '</p></div>';
}
m2(b,f);
}
else if (h===1) // on tweet fail - add in graceful fail <xx_f>
{;}
else if (h===2)
{
a=a.substr(6);
alert('php error: ' + a); // on php error
}
}
/*
Ajax call-back methods - do nothing but report a PHP error if it ocurred. On a success of and add or delete there is no message to the user. Need to add in a graceful fail, ie. <xx_f>.
*/
function ajax_bookmark(a,b)
{
c=a.slice(0,6);
d=check_aml(c);
if(d===0)
{;}
else if (d===1)
{;}
else if (d===2)
{
a=a.substr(6);
alert('php error: ' + a);
}
}
function ajax_bookmark_add(a,b)
{
ajax_bookmark(a,b);
}
function ajax_bookmark_delete(a,b)
{
ajax_bookmark(a,b);
}
/*
Place holder for method parameter
*/
function ajax_null()
{
}

Look into groupping using the module pattern or regular object instatination.

Related

I am not able to run external library

I am having a little difficulty. I believe it's because of my lack of knowledge. The fact is, i have a external library, to run with a external hardware. This library is of that hardware and run some events. I ain't able to understand how to run this events in my program. follow the code that i am trying to use.
//An example of an implemented proxcard callback is as follows:
grabba.proxcard.registerCallback(proxcardCallbacks, onError);
var proxcardTimeoutFunction = function() {
alert('Proxcard timed out');
};
var proxcardCallbacks = {
//Functions may also be declared inline as seen here.
triggeredEvent: function() {
alert('Proxcard triggered');
},
//These functions are optional and unimplemented functions will simply not be called.
//stoppedEvent : function () {
//alert('Proxcard scanning stopped');
//},
scannedEvent: function(proxcard) {
//proxcard contains data fields
//Convert the data to a base 16 hex string
var hexString = ons.grabba.util.integerArrayToHexString(proxcard.data);
alert('Proxcard scanned event\n Data: ' + hexString + '\n' + "Type: " + proxcard.type);
},
//You may create a function separately as seen with proxcardTimeoutFunction here.
timeoutEvent: proxcardTimeoutFunction
};
I need to scan with the RFID of that hardware. For more information's, follow the documentation of the library:
http://cordova.grabba.com/legacy/versions/v1.0.18/GrabbaProxcard.html
Thank you for your help.
This example is not working as is, you have to replace the "proxcardCallbacks" and "onError" parameters with your own functions.
Try this:
<button onclick="grabba.proxcard.registerCallback({ //Functions may also be declared inline as seen here.
triggeredEvent: function () {
alert('Proxcard triggered');
},
//These functions are optional and unimplemented functions will simply not be called.
//stoppedEvent : function () {
//alert('Proxcard scanning stopped');
//},
scannedEvent: function (proxcard) {
////proxcard contains data fields
////Convert the data to a base 16 hex string
try {
//alert('data: ' + proxcard.data);
var hexString = grabba.util.integerArrayToHexString(proxcard.data);
alert('Proxcard scanned event\n Data: ' + hexString + '\n');
} catch (err) {
alert('catch error ' + err);
}
},
timeoutEvent: function () {
alert('Proxcard timed out');
}
}, function (errorString) {
alert('on error ' + errorString);
});">Scan</button>
PS: Notice that I removed the "ons." from the begging of the "integerArrayToHexString" function.

Retrieve Multiple Enitiies in Dynamics CRM using OData

I have a ribbon button command which executes a javascript function and passes in the selected rows in a grid. I am looping through that list to create a $select filter to make a RetrieveMultiple request.
The problem is everytime I get the following error
400: Bad Request: No Property 'id' exists in type 'Microsoft.Xrm.Sdk.Entity' at position 1
I have tried with id instead of Id but I still get the same error.
My code is below
function approveMultipleApplications(selectedApplicationReferences) {
if (selectedApplicationReferences && selectedApplicationReferences.length > 0) {
var filter = '';
for (var i = 0; i < selectedApplicationReferences.length; i++) {
filter += '(id eq guid\'' + selectedApplicationReferences[i].Id + '\')';
if (i < selectedApplicationReferences.length - 1) {
filter += ' or ';
}
}
var options = "$select=new_assessmentcount,new_requiredassessmentcount&$filter=" + filter;
try {
SDK.REST.retrieveMultipleRecords("new_application", options, retrieveApplicationsCallBack, function (error) {
alert(error.message);
}, retrieveComplete);
}
catch (ex) {
Xrm.Utility.alertDialog('Something went wrong, please try again or contact your administrator ' + ex, null);
}
}
else {
Xrm.Utility.alertDialog('You must select at least one application to approve', null);
}
}
The selectedApplicationReferences[i].Id is in this format {guid-value}
Any help or guidance is appreciated
The error message is pretty much spot on: Use LogicalNameId instead of just Id. In your case that would be new_applicationId:
filter += '(new_applicationId eq guid\'' + selectedApplicationReferences[i].Id + '\')';
It can be a bit confusing since there is actually no Id-field in the database. If you use e.g. early bound classes, the Id field is set for you behind the scenes, so that might have confused you. The Id field is not returned by the OData endpoint.

How to invoke C++/Qt function from Javascript in QWebengine?

I have looked the Qt example about QWebChannel .
But its a C/S app. BTW it doesn't work on my computer(OS X 10.10.5, Qt5.5).
All my HTML contents come from local disk. I don't think it needs socket things.
new QWebChannel(navigator.qtWebChannelTransport, function(channel) {
// all published objects are available in channel.objects under
// the identifier set in their attached WebChannel.id property
var foo = channel.objects.foo;
// access a property
alert(foo.hello);
// connect to a signal
foo.someSignal.connect(function(message) {
alert("Got signal: " + message);
});
// invoke a method, and receive the return value asynchronously
foo.someMethod("bar", function(ret) {
alert("Got return value: " + ret);
});
});
There is no
navigator.qtWebChannelTransport
such a thing, JS stopped there.
What should I fill with the first parameter ?
For QtWebEngine, you have to use qt.webChannelTransport.

Node.js print "undefined" in a impossible place

In my code, every place where something is printed, it goes througth this function:
log = function log (LOG, message) {
if (!message) {
message = LOG;
LOG = '\t';
}
console.log('#' + NAME + '\t-\t' + resize(LOG, 12) + '\t:\t' + message);
}
(The why of this function is not the question and is for formatting).
So every new line begin with #, whatever what data is supposed to be printed after this.
But in my log, I have this:
#App - play : [ 'FC4.mp4',
'ME.mp4',
'ME2.mp4',
'MER.mp4',
'PvS.mp4',
'BF3.mp4',
'BF4.mp4',
'BFH.mp4',
'BFH2.mp4',
[length]: 9 ]
undefined
#App - play : at 2015-03-17_16-56
Commenting out line by line just make undefined appear sooner:
#App - update : Done
undefined
#App - play : at 2015-03-17_17-08
There is no use of console.log, process.stdout.write or other write function anywhere in the code, and there is no character \n used anywhere in the code.
How can undefined appear like that? What possible cause can there be? It just freak me out to see this appear in the same place in the log whatever I do.
I ve calmed down too late to prevent posting a stupid question, but the line came from omxdirector, a npm module that I didn't checked completely before use.
Thanks to you.
(The line is in omxdirector/main.js:
var sendAction = function (action) {
if (commands[action] && omxProcess) {
try {
omxProcess.stdin.write(commands[action], function (err) {
console.log(err);
});
} catch (err) {
console.log(err);
}
}
};
Once modified, everything is back to normal)
It looks to me like NAME may be undefined for one call. You can test it by doing this:
if(NAME) {
console.log('#' + NAME + '\t-\t' + resize(LOG, 12) + '\t:\t' + message);
}

What is the equivalent of this Coffescript code in Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
phantom = require 'phantom'
phantom.create (ph) ->
ph.createPage (page) ->
page.open "http://www.google.com", (status) ->
console.log "opened google? ", status
page.evaluate (-> document.title), (result) ->
console.log 'Page title is ' + result
ph.exit()
I tried using this website but it doesn't seem to be very accurate. It has returns everywhere. http://js2coffee.org/#coffee2js
Update: After a second look, it does seem that some of these returns are spurious/redundant. That is because Coffeescript just always returns the result of the last statement in the function (so that you can save the return keyword), even in cases where you would not have returned anything in Javascript (the compiler cannot know your intention here). That may be unnecessary, but there is also no harm in it, if no one was using the return value anyway. If it is somehow important to return "nothing", you'd can explicitly do that, too.
You can just compile it, to see what it results in:
var phantom;
phantom = require('phantom');
phantom.create(function(ph) {
return ph.createPage(function(page) {
return page.open("http://www.google.com", function(status) {
console.log("opened google? ", status);
return page.evaluate((function() {
return document.title;
}), function(result) {
console.log('Page title is ' + result);
return ph.exit();
});
});
});
});
It has returns everywhere.
Well, every function you define there has one return.
One of the prime motivators for Coffeescript is to be able to write all those callback functions with less boilerplate.
Either way, the compiler is "accurate".
var phantom = require('phantom');
phantom.create(function(ph)) {
ph.createPage(function(page) {
page.open("http://www.google.com", function(status) {
console.log("opened google? ", status);
page.evaluate(function() { return document.title; }, function() {
console.log('Page title is ' + result);
ph.exit()
}
});
});
});

Categories