I'm rather new to jQuery and I don't understand why this loop doesn't display
the object properties.
I would be grateful if you could help me.
var shop_array = ["title","price","img","text"];
var submit = $(".add").find(":submit");
submit.on("click",function(e){
var elements = $(".add").children(':input');
for(var i=0;i<elements.length;i++){
if($(elements[i]).val()!==""){
var object = '\"'+shop_array[i]+'\"';
console.log(shopcart.shop_values[object])//dosen't display shop_value;
console.log(object);
}
}
});
var shopcart= {
shop_values :{
"title":"a",
"price":"b",
"img":"img",
"text":"text"
},
add: function(){
}
}
Your problem is the " you put around the variable you want to use to access the object properties, they should not be there as they are not in the property names
var object = '\"'+shop_array[i]+'\"';
console.log(shopcart.shop_values[object])//dosen't display shop_value;
should just be
console.log(shopcart.shop_values[shop_array[i]])//dosen't display shop_value;
For your current code to have worked the shopcart would need to be defined as
var shopcart= {
shop_values :{
"\"title\"":"a",
"\"price\"":"b",
"\"img\"":"img",
"\"text\"":"text"
},
add: function(){
}
}
which would look just awful.
Related
I have a problem with the following code:
// At the beginning
var prog = {}
// some attributes of prog and other methods
// ...
prog.stateChange = function(state)
{
var functionOfState =
{
onState1: function()
{
// some code
},
onState2: function()
{
// some code
}
}['on'+state]()
}
Which purpose have these square brackets after the creation of the object functionOfState? Is this an array of possible methods?
Sorry, I'm a total newbie in JS and I haven't found any information about this.
I really appreciate any help.
This code does almost the same as:
var functionOfState =
{
onState1: function()
{
// some code
},
onState2: function()
{
// some code
}
}
functionOfState['on'+state]();
It simply creates an object which stores different functions. Then, it calls one of them according to the current value of state.
Maybe, this one will be even easier:
var functionOfState = {};
functionOfState['onState1'] = function() {
// someCode
};
functionOfState['onState2'] = function() {
// someCode
};
functionOfState['on'+state](); // when state is 'State2', 'onState2' will be called
The difference that in your code it doesn't store this object with functions, but calls it directly.
This is (not the most clear) way to extract field from an object.
in JS, the subscript operator ([]) can extract a property out of an object just like the dot operator (.), so the following expressions are equal:
var obj = { field : value };
obj.field == obj["field"]; //returns true
on your example an object with the fields onState1, onState2 is created. then, using the subscript operator the correct property is extract. it is equivilant on writing
prog.stateChange = function(state)
{
var temp =
{
onState1: function()
{
// some code
},
onState2: function()
{
// some code
}
};
var functionOfState = state == onState1 ? temp.onState1 : temp.onState2;
functionOfState();
}
This is not a legit way to extract a property/method out of an object. basically if someone changes the name of the method, the code breaks. it is much better to simply use a switch case.
I have a knockoutJS observable array which I created in the usual way:
var MyGridModel = function(items) {
var self = this;
this.items = ko.observableArray(items);
...
I would like to change the array to a new one based on new information. I need to change the entire array (or if I can, the entire contents to the new array, whichever is easier).
My attempt to do this is done in the following code:
this.setData = function(newData)
{
var grid = ko.observableArray(newData);
self.items([]);
self.items(grid);
}
Unfortunately, I am getting a blank grid when I perform this operation.
Clearly I am missing something. How do I make this change work?
Someone please advise...
To update the contents of an observable array, just call it and pass the new contents to it:
this.setData = function(newData) {
self.items(newData);
}
Try this:
this.setData = function(newData)
{
self.items.removeAll();
for(int i=0; i<newData.length; i++){
self.items.push(newData[i]);
}
}
Note: Each push will update observers.
My second example is the one returning [function getValue] and I'm trying to fix it but I can't see what the problem is.
I've been messing around with xmlparse in google scripts, the xml I want to parse keeps all relevant data in the attributes of elements.
Here's a sample of that xml format: https://api.eveonline.com/account/characters.xml.aspx?keyID=1409941&vCode=xagxMH966J2EQinVpoFOBB5H1UidCwsjoTwtBKhhvMVZWqq6Jio4mkiBwv026Olc
Here's some code that works (displayed via log [ctrl]+[enter]):
function dialogDisplay() {
var xmlstring = Xml.parse('<rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID"><row name="Jonah Younbrog" characterID="90131303" corporationName="House of Praetor" corporationID="523373135"/><row name="Mador Younbrog" characterID="90149709" corporationName="House of Praetor" corporationID="523373135"/><row name="Marc Younbrog" characterID="747451028" corporationName="House of Praetor" corporationID="523373135"/></rowset>');
var attributes = xmlstring.getElement().getAttributes();
for (var i in attributes) {
Logger.log(attributes[i].getValue());
}
}
And here's the code that doesn't work, it also logs the element names (successfully) and uses nested fors to go through the xml:
function fetchToLogger() {
var assetURL = "https://api.eveonline.com/account/characters.xml.aspx?keyID=1409941&vCode=xagxMH966J2EQinVpoFOBB5H1UidCwsjoTwtBKhhvMVZWqq6Jio4mkiBwv026Olc";
var assetstring = UrlFetchApp.fetch(assetURL).getContentText();
var xmlstring = Xml.parse(assetstring, false);
var elements = xmlstring.eveapi.result.getElements();
for (var a in elements) {
Logger.log(elements[a].getName().getLocalName());
var attributes = elements[a].getAttributes();
for (var x in attributes) {
Logger.log(attributes[x].getValue);
}
var subelements = elements[a].getElements();
for (var b in subelements) {
Logger.log(subelements[b].getName().getLocalName());
var subattributes = subelements[b].getAttributes();
for (var y in attributes) {
Logger.log(attributes[y].getValue);
}
}
}
}
.getValue is a function. so you should use .getValue()
For example:
Logger.log(attributes[x].getValue());
You are not calling the actual function, change to:
Logger.log(attributes[x].getValue());
Note that Logger.log(attributes[x].getValue is just a reference to a function, and this is what console shows.
Is it possible to create nested objects figure in graphiti.js?
However I was able to do that using the decoration approach and it works well.
ivr.shape.menu.Menu = graphiti.shape.basic.Rectangle.extend(
{
TitleLocator : graphiti.layout.locator.Locator.extend({
init: function(parent) { this._super(parent); },
relocate:function(index, figure){
var bb = this.getParent().getBoundingBox();
var tbb = figure.getBoundingBox();
figure.setPosition(bb.getWidth()/2-tbb.getWidth()/2,-10);
}
}),
init: function( strTitle ) {
this.subElements = {};
this.subElements.title = new graphiti.shape.basic.Label(strTitle);
this.addFigure(this.subElements.title, new this.TitleLocator(this));
this.calculateSize();
}
}
How wrong is that?
nested figures are not supported at the moment.
Sorry
Andreas
I have the following (simplified) object literal. The icons method uses closure to hide the icons variable, which I'd like to have as an associative array for later lookups.
var MapListings = {
icons: function () {
var allIcons = [] ;
return {
add: function (iconType, iconImage) {
var icon = new GIcon(MapListings.baseIcon);
icon.image = iconImage;
allIcons[iconType] = icon; // fails, but this is what I want
// allIcons.push(icon); // works, but this is not what I want
},
get: function () {
return allIcons;
}
};
} ()
}
I add items to the to the icons object like so:
MapListings.icons.add("c7", "/images/maps/blue.png");
MapListings.icons.add("c8", "/images/maps/red.png");
The following doesn't work:
allIcons[iconType] = icon;
But this does:
allIcons.push(icon);
Outside of the closure the associative array style works fine, so perhaps there is a conflict with jQuery? The error I get in firebug a is undefined looks to come from the library. I'd like to maintain the associative array style.
Any ideas?
Update
It looks like this conflict is coming from google maps. Odd, not sure of a way around this.
Dumbass Update
The part of my object literal that returned a base GIcon() object wasn't returning an object at all. So, the object didn't have the right properties.
baseIcon: function () {
var base = new GIcon();
base.shadow = '/images/maps/shadow.png';
base.iconSize = new GSize(12, 20);
base.shadowSize = new GSize(22, 20);
base.iconAnchor = new GPoint(6, 20);
base.infoWindowAnchor = new GPoint(5, 1);
return base;
}
And MapListings.baseIcon is NOT the same as MapListings.baseIcon()! D'oh
if you want a lookup table, just do var allIcons = {}
EDIT: Though technically it should work either way, as an array IS an object. Are you sure there isn't more to this?
EDIT #2: Can't you just make allIcons as a property of MapListings?
EDIT #3: I think it's working, but maybe you're not accessing it right? That or it fails creating the object with Google somehow, or the error you posted is happening elsewhere, and not here
function GIcon(){};
var MapListings = {
icons: function () {
var allIcons = [] ;
return {
add: function (iconType, iconImage) {
var icon = new GIcon(MapListings.baseIcon);
icon.image = iconImage;
allIcons[iconType] = icon; // fails, but this is what I want
// allIcons.push(icon); // works, but this is not what I want
window.x = allIcons
},
get: function () {
return allIcons;
}
};
} ()
};
MapListings.icons.add("c7", "/images/maps/blue.png");
MapListings.icons.add("c8", "/images/maps/red.png");
alert( MapListings.icons.get()['c8']['image'] )
You shouldn't loop using .length but instead directly access c7 or c8.
x = MapListings.icons.get();
for ( var prop in x ) {
if ( x.hasOwnProperty(prop ) ) {
alert( x[prop]['image'] )
}
}
So one thing you could do to fix this is change the way you reference the array. Since external to your add method you do this:
MapListings.icons["c7"]
You can also just use this to add to your array inside your add function:
add: function (iconType, iconImage) {
MapListings.icons[iconType] = iconImage;
},
allIcons[iconType] = icon; fails because allIcons is an Array, not an object. Try initializing allIcons to {} instead. That would allow you to place items in the collection by key.