I have a function to find highest value in an XML file from certain tag which has to assign one of default value in plugin. My problem is my plugin code runs before the other function hence a null value is returned. Can I run function get_Highest_Property_Prise() before plugin, like java constructor? Or some how initialize a global variable before plugin code kicks in?
var pulgin_Global_Variables = {
hight_price: ""
};
(function($) {
$.fn.SearchProperty = function (options) {
var defaults = {
S_MinPrice: 0,
S_MaxPrice: get_Highest_Property_Prise()
};
alert("yo yo "+defaults.S_MaxPrice);
}
})(jQuery);
function get_Highest_Property_Prise()
{
$.get('Data.xml', function (XML_property) {
$(XML_property).find('property').each(function () {
var c_Price = parseInt($(this).find('priceask').text().replace(',', ''));
if (c_Price > pulgin_Global_Variables.hight_price) {
pulgin_Global_Variables.hight_price = c_Price;
}
}); //end of function
});
}
var pulgin_Global_Variables = {
hight_price: ""
};
$.fn.SearchProperty = function (options) {
var defaults = {
S_MinPrice: 0,
S_MaxPrice: pulgin_Global_Variables.hight_price
};
alert("yo yo "+defaults.S_MaxPrice);
}
})(jQuery);
//here set max price to match your global object
$.get('Data.xml', function (XML_property) {
$(XML_property).find('property').each(function () {
var c_Price = parseInt($(this).find('priceask').text().replace(',', ''));
if (c_Price > pulgin_Global_Variables.hight_price) {
pulgin_Global_Variables.hight_price = c_Price;
}
}); //end of function
}).done(function () {
$(whatever).SearchProperty()
})
Related
In an Ionic-app i have a problem with one of my JavaScript functions which is called by another one.
I always get an error, that the function getSqlSelect is not defined.
But the functions are all defined in the services.js script.
Here's a short version of the code from services.js:
.factory('LocalDatabase', function () {
var arrResult = "";
return {
select: function (table, filterAttributesArr, success) {
var sql = getSqlSelect(table, filterAttributesArr);
var db = window.sqlitePlugin.openDatabase({ name: 'p16.sqlite', location: 0 });
},
getSqlSelect: function (tablename, filterAttributesArr) {
return "";
}
};
})
That's because you don't have a getSqlSelect variable in scope in that location. If select is called as obj.select(...) where obj is that object returned by the callback to factory, then within it you can use this.getSqlSelect(...) to refer to it. E.g.
var sql = this.getSqlSelect(table, filterAttributesArr);
// ^^^^^
If select isn't called with the right this, that won't work. If you don't need to expose it, just define it within your callback:
.factory('LocalDatabase', function () {
var arrResult = "";
function getSqlSelect(tablename, filterAttributesArr) {
return "";
}
return {
select: function (table, filterAttributesArr, success) {
var sql = getSqlSelect(table, filterAttributesArr);
var db = window.sqlitePlugin.openDatabase({ name: 'p16.sqlite', location: 0 });
}
};
})
If select isn't called with the right this and you do need to expose getSqlSelect, you can do that too:
.factory('LocalDatabase', function () {
var arrResult = "";
function getSqlSelect(tablename, filterAttributesArr) {
return "";
}
return {
select: function (table, filterAttributesArr, success) {
var sql = getSqlSelect(table, filterAttributesArr);
var db = window.sqlitePlugin.openDatabase({ name: 'p16.sqlite', location: 0 });
},
getSqlSelect: getSqlSelect
};
})
var Rules = Rules || (function () {
saverule = function () {
var level = document.getElementById("level-selection");
var metrics = document.getElementById("metric-selection");
var operator = document.getElementById("operator-selection");
var value = document.getElementById("value123");
var saveAction = $("#hidden-save").val();
$.post(saveAction, { level_id: level, product_id: metrics, opp: operator, value: value }, function () {
},
'json');
};
wireLinkActions = function () {
$("a.save-ok").on("click", function(event) {
saverule();
return false;
});
};
return {
Initialize: function () {
wireLinkActions();
}
}
})();
$(document).ready(Rules.Initialize);
illegal invocation error it wont even cal the the save rule function while debugging also
Make sure your return the values and not the DOM element itself.
For example change this:
var level = document.getElementById("level-selection");
into this:
var level = document.getElementById("level-selection").value;
or, simply use jQuery such as this:
var level = $("#level-selection").val();
I am trying to write a small javascript library as shown below. What I really want is when I call
console.log(tnd().pv);
it should output same number and not generate new number everytime. I know the issue is it calls Math.random everytime I console log. But how can I do so that it outputs same number?
(function () {
var tnd = function() {
return new tnlib();
};
var tnlib = function() {
this.version = function(){
console.log('1.0');
};
this.pv = Math.random()*10000000000000000;
};
if(!window.tnd) {
window.tnd = tnd;
}
})();
Don't execute Math.random() on each invocation of tnlib, but as a static variable:
(function () {
function tnd() {
return new tnlib();
}
function tnlib() {
}
tnlib.prototype.version = function(){
console.log('1.0');
};
tnlib.prototype.pv = Math.random()*10000000000000000;
if (!window.tnd) {
window.tnd = tnd;
}
}());
(or, if you really need to make pv an instance property):
var staticPv = Math.random()*10000000000000000;
function tnlib() {
this.pv = staticPv;
…
}
How do I send parameters to self invoking functions. like following code.
MyNameSpace.Administration.WebAdministrator = function () {
var init = function () {
//some code here
},
CreateSubSite = function (id, url) {
//some code here
}
return {
start: function () {
init();
},
createSubSite: CreateSubSite(myId, myUrl)
}();
$(document).ready(function () {
MyNameSpace.Administration.WebAdministrator.start();
$("div.large-app").on("click", "#btnCreate", function () {
var id = "something";
var url = "something";
MyNameSpace.Administration.WebAdministrator.createSubSite(id, url);
});
});
When I add this code on my js file and run the web application it says myId and myUrl not defind and not runing the js code.
Looks like you're really trying to do this
var MyNameSpace = {
Administration : {
WebAdministrator : {
init : function () {
//some code here
},
CreateSubSite : function (id, url) {
//some code here
},
start : function () {
this.init(); // really ?
}
}
}
};
$(document).ready(function () {
MyNameSpace.Administration.WebAdministrator.start();
$("div.large-app").on("click", "#btnCreate", function () {
var id = "something";
var url = "something";
MyNameSpace.Administration.WebAdministrator.CreateSubSite(id, url);
});
});
FIDDLE
I'm having some trouble working out how to control program flow and not use global variables with the Javascript in my web app. In this example, when get_notes() is called, the ids of the received notes are stored in the current_note_ids array. When add_to_discussion() is called, current_note_ids is sent as a parameter to the server request. How can I do this without having current_note_ids as a global variable?
<script type="text/javascript">
var current_note_ids = [];
function add_to_discussion(){
$.post('/add_to_discussion',{current_note_ids:current_note_ids});
}
function get_notes(){
$.post('/get_note_combination',{}, function(data) {
current_note_ids = []; // clear existing note details
for (i in data.notes) {
current_note_ids.push(data.notes[i].guid);
}
}
}
$(document).ready(function() {
$('#add_to_discussion_button').click(function(){
add_to_discussion();
return false;
});
$('#get_notes_link').click(function(){
get_notes();
return false;
});
});
</script>
This removes all that code from the global scope using a closure
(function () {
var current_note_ids = [];
function add_to_discussion(){
$.post('/add_to_discussion',{current_note_ids:current_note_ids});
}
function get_notes(){
$.post('/get_note_combination',{}, function(data) {
current_note_ids = []; // clear existing note details
for (i in data.notes) {
current_note_ids.push(data.notes[i].guid);
}
}
}
$(document).ready(function() {
$('#add_to_discussion_button').click(function(){
add_to_discussion();
return false;
});
$('#get_notes_link').click(function(){
get_notes();
return false;
});
});
})();
You can use anonymous function to make it OO-like. In this case, you can choose what to "expose".
var notes = $(function() {
var current_note_ids = [];
function add_to_discussion() {
$.post('/add_to_discussion', {
current_note_ids: current_note_ids
});
}
function get_notes() {
$.post('/get_note_combination', {}, function(data) {
current_note_ids = []; // clear existing note details
for (i in data.notes) {
current_note_ids.push(data.notes[i].guid);
}
})
}
return {
add_to_discussion: add_to_discussion,
get_notes: get_notes
};
})();
$(document).ready(function() {
$('#add_to_discussion_button').click(function() {
notes.add_to_discussion();
return false;
});
$('#get_notes_link').click(function() {
notes.get_notes();
return false;
});
});