How do I access data in this javascript MAP object? - javascript

I'm making an AJAX call which returns XML data, and this is my 'success:' function (callback):
success: function (data) {
var $rowArray = $(data).find("[nodeName=z:row]");
$rowArray.each(function(index) { // for each date put it into calMap.
calMap[$(this)[index].title] = $(this).attr("ows_Title");
calMap[$(this)[index].date] = $(this).attr("ows_EventDate");
});
}
calMap is a global javascript object declared outside of the function.
var calMap = {};
What I want to do is create a function where I can pass in a title, have it search calMap for that title, and if found, the specific object is returned and I'll be able to access the date property for that object.
Problem is, I can't seem to access the data I insert into the calMap object. For starters, I just want to print the map. Tried eval'ing it, tried alerting calMap[0], tried alerting calMap[0].title, but nothing. Can someone help me with this? Thanks!
Update:
I want to do something like this:
var data = getData("myTitle");
function getData(title) {
// if title is in calMap, something like this?
var result = (calMap[title]));
return result; // returns an object or NOTHING
}
then i'll check if date is defined or not, and if it is, i'll access its properties (ie. data.date. That make sense?
ANSWER:
I ended up using an array. STILL think I should be able to use the object MAP, but needed to get my project done.
Here's the final code for the code that accesses the array items:
function hasCalDate(code)
{
var matched = "";
for (var f=0;f<calMap.length;f++){
var re = new RegExp(code);
if (re.test(calMap[f].title))
{
matched = calMap[f].title+','+calMap[f].date;
}
}
return matched;
};
Thanks everyone.

You need to initialize calMap as an array (i.e. square brackets, not curly ones):
var calMap = [];
Then, inside your each function, I'm guessing you want something more like
calMap.push({
title: $(this).attr("ows_Title"),
date: $(this).attr("ows_EventDate")
});

Your problem is that the success function is only run when your AJAX request completes. If you want to access calMap safely, you need to do so inside your callback.

Related

Callback - ReferenceError: Variable is not defined

I am trying to get some parameters form the URL and pass them on to another function.
The function for getting the parameters is like that:
function getUrlVars(callback) {
var urlVars = {};
var urlparts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
urlVars[key] = value;
});
callback(urlVars);
}
It gets the parameters just fine. The function that urlVars should be passed to, looks like this:
document.addEventListener('done', function() {
var projSelect = document.getElementById('projectSelection');
projSelect.value = urlVars[0];
});
});
};
I am calling both functions like so:
window.onload=function(){
getUrlVars(selectBox(urlVars));}
So all I am trying to do, is set the value of a selectbox according to a parameter in the URL. But I get an error, telling me that "urlVars" ist not defined.
And quite frankly, I am at my wits end. Can anybody tell me what I did wrong?
The problem you are having, is that when you are defining the method for window.onload you are passing a parameter that is not defined anywhere in your script.
Furthermore, it seems like you want to pass a callback method to urlVars, but you are passing a parameter? It is hard to tell by the code that you have provided.
Why not get the parameters you want from your select box and then send them to the desired method?

Create variable from data element on page

We have a script run early on our page that feeds values to a third party.
foo.bar.myPage({
"myId": "12345",
"myName": "My name is Joe",
});
I want to create a new variable using one of those values and use it for something else. I can't figure out how to grab the value from above. I do not have access to the code above - need to do this using javascript or jquery via a CMS. Help?
var valueFromMyId = $(myId);
alert (valueFromMyId);
Obviously I want "12345" to fire in my alert.
I Tried something like this:
foo = {
"bar":{
"myPage":function(param){
console.log("inside"+param);
}
}};
foo.bar.myPage(123); //print inside 123
Now Try this to override, if you can access foo.bar.
var backup = foo.bar.myPage
foo.bar.myPage = function(param){
console.log("Using params outside the actual function "+ param);
//calling the actual function which was getting called initially
backup(param);
}
now every foo.bar.myPage call will go through your define method, that way you can access param before the actual function gets it.
This might not be the best way to do it. But will work if you have access to foo.bar object. Test and let me know if this helps.

Accessing var function within function in another file

I have 2 JS files - one with the functions I would like to access and the other that I'd like to call the function with.
(function($) {
var Place = function() {
var Location = function(id, duration, check) {
//Should access this function
}
}
})(jQuery);
I'm trying to access it with:
Place.Location(markerId, 600);
But all I'm getting is that it's not defined. Simple issue but can't quite figure this one out.
As it's a jQuery plugin, maybe there's a way I can access it via another method?
$.fn.place = function(params) {
var len = this.length;
return this.each(function(index) {
var me = $(this),
key = 'place' + (len > 1 ? '-' + ++index : ''),
instance = (new Place).init(me, params);
});
};
The way you are defining Location, it is a private variable inside the function Place. If you want to access it as an attribute of Place, you should replace var Location = ... with this.Location = ...
It's going out of scope. Because you wrapped your Place object in function($) {}, now anything outside that wrapper will no longer have access to variables inside the wrapper. If $ stands for jQuery, it should be a global anyways and you can take the wrapper out.
The solution is a combination of the other two answers.
You define Place as a variable in the (anonymous) function. It can't be used outside the scope of that function. (This function doesn't use jQuery, either, so the wrapper is unnecessary).
Place is a function. It executes code that sets local variable Location to a function, but doesn't export that function, so Location() is inaccessible outside the Place function.
You probably mean to make Place an object (instead of a function), and give it a Location method. Here's one way to write it:
var Place = {
Location: function(id, duration, check) {
// do something with id, duration, & check
}
};
// execute
Place.Location(someId, someDuration, someCheck);
(It doesn't look like you've posted all your code, like the Place.init() method, but there are plenty of ways to write this so that it works correctly; this should solve your immediate problem.)

pass parameter to a prototyped function in javascript?

How can I pass an object from a function to a protoyped function of it's own?
function Main()
{
this.my_object = {"key":123};
}
Main.prototype.Sub = new Sub(this.my_object);
function Sub(obj)
{
alert(obj);
}
Main.Sub; //this should alert the object created in Main()
fiddle: http://jsfiddle.net/GkHc4/
EDIT 1:
I'm trying to make a chain of functions, and each link must get the previous object and add something. At this point it is an experiment. For example:
Main.Link1.Link2.link3();
//link3 it's a prototype for link2
//link2 it's a prototype for link1
//and so on...
Where each link adds a key to the initial object
There are three different issues:
1) You don't create an object with new Main(), but try to access the Sub property directly from the constructor. This doesn't work. You have to create an instance:
var main = new Main();
main.Sub; //<-- now you can access it
2) You try to access the property my_object with this, but outside of any function. That doesn't work either. this will probably point to the window object, which doesn't have any property called my_object. The solution could be to write main.my_object but that would kind of defeat the purpose of the prototype. Usally you would put there function or properties that are the same for every instance. But you are trying to put a property in there that should be different for every instance. So it looks like you don't need to access the prototype at all but can just define it as a regular property:
function Main()
{
this.my_object = {"key":123};
this.Sub = new Sub(this.my_object);
}
3) The line main.Sub doesn't execute anything. You are just requesting the property Sub. Instead the function Sub will be executed when you write new Sub(...). So if you want to alert something by calling a function, you have to define a function. You could for instance define an alert method in Sub or in Sub.prototype and then call this method:
function Sub(obj)
{
this.alert() {
alert(obj);
}
}
main.Sub.alert();
updated Fiddle
I think that maybe you are looking for something like the following:
function Main()
{
this.my_object = {"key":123};
}
Main.prototype.Sub = function () {
Sub(this.my_object);
};
function Sub(obj)
{
alert(obj);
}
var main = new Main(); // main object is created with main.my_object property
main.Sub(); // this will do alert(main.my_object)
I think you're going at it in the wrong way.. you see:
The alert is not coming from the last line, it's actually coming from the prototype line, when you do the "new Sub".
Maybe a better approach would be something like:
function Main()
{
this.my_object = {"key":123};
}
Main.prototype.Sub = Sub; //You set the prototype, but don't actually execute the function
function Sub(obj)
{
alert(obj);
}
var m = new Main(); //You need to create a new object of type Main in order for it to have access to the method Sub
m.Sub(m.my_object); //this should alert the object created in new Main()
Does this help?
Edit
Additionally, you could even do something like this for the Sub function:
function Sub() {
alert(this.my_object);
}
Although that way, you wouldn't be able to use the function by itself.

How do you save objects into a google Ui instance.

What I want to do is save a data-table and an array in the UI instatance, so that the Handler can call on them to do what it is supposed to do. I am fairly new to programming so don't feel bad if you treat this like a stupid question, but please still answer it. Thank you.
One of the easiest way is to use a hidden widget to store data as string like in this example :
function doGet() {
var app = UiApp.createApplication();
var hidden = app.createHidden('hidden').setId('hidden');// widget's name = hidden
//...
var array = ['item1','item2'] ;
hidden.setValue(array.toString()); //assign a value as a string
//...
return app
}
function handlerfunction(e){
var array = e.parameter.hidden.split(','); // get the widget's value by its name parameter and reconvert it back to an array
//...
return app
}

Categories

Resources