TaffyDB: Select problems - javascript

I'm new to TaffyDB and haven't done a lot of javascript programming so I'm hoping that the problem I'm having is something simple. I'm trying to update a listbox with the options stored in the TaffyDB according to the selected client. When I do my select however, it is returning all the rows.
Below is the code I am using to update the listbox, along with the selectString used to do the query, and what's in the TaffyDB.
Anyone have any ideas why I am getting back all rows when I specify clientID = 1788?
I tried the select string with and without quotes around the column identifier.
// load existing user client projects if we have any
var lbProjects = document.getElementById('lbProjects');
lbProjects.options.length = 0;
var selectString = '{clientID:"' + clientID + '"}';
alert(selectString);
userProjects(selectString).each(
function (r) {
var option = new Option();
option.value = r.projectID;
option.text = r.projectName;
lbProjects.add(option, null);
});
What's in selectString:
{clientID:"1788"}
What's in the DB:
[{"clientID":"1788","projectID":"19"},
{"clientID":"1789","projectID":"24"},
{"clientID":"1790","projectID":"23"}]
Thanks for any help.
Aaron L. Bratcher

The problem was trying to use the selectString variable.
The line
userProjects(selectString).each(
now reads
userProjects({clientID: clientIDValue}).each(
I was supposed to be passing in an object array, not a string. {} in javascript creates an array of objects.

Related

Javascript in HAML in Javascript

I've got a question that mightve been asked before but i have trouble finding a proper description. I hope someone can help me out.
In the code below on the line where i set var price i want to add the javascript variable accu_id to find a record in my DB through rails. How do I do this in embedded rails in javascript?
:javascript
$('#accu').change(function() {
var accu_id = $("#accu")[0].selectedOptions[0].value
var price = "#{currency(Product.find_by(id: accu_id).price2)}"
$('#price_accu').append(price)
});
Or is there an easier way to do this?
You have two options.
You can use AJAX request to get data from database
You can use mapping concept like following
var data = #processed_data // json data, which contains processed data in json format.
var accu_id = $("#accu")[0].selectedOptions[0].value
var price = data.accu_id;
Here the #processed_data contains data like following
#processed_data = currency_calculation
def currency_calculation
Product.all.map { |product| [product.id, currency(product.price2) ] }.as_json
end
Example
Assume products table have two entries then the #processed_data contain values like following
{ "1" => "20.00", "2" => "25.50" }
The above data directly assigned to js variable data and accessed like data.key
The first option is best choice and second is possible one.
Note : You can't use js variable inside ruby.

Submitting multiple inputs with same name

Ok, so I have form for creating polls. I want to use AJAX request and able user to attach image instead of question, so I use FormData for this.
I could't find any solution for working with multiple input with same name (named like this: "name[]"). I tried this option:
var fdata = new FormData();
fdata.append('answers[]', $('input[name="answer[]"]').val());
But it doesn't work. I know I could use .each(), but I don't want different name for each question, so I don't have to rebuild PHP side too much.
Thanks for any help.
You have to append each value in turn. Currently you are only appending the first one (because that is what val() returns.
$('input[name="answer[]"]').each(function (index, member) {
var value = $(member).val();
fdata.append('answers[]', value);
});
The problem is $('input[name="answer[]"]').val() isn't giving you what you need; it returns the first input element's value. Instead, you want an array of values:
var values = [];
$('input[name="answer[]"]').each(function(i, item) {
values.push(item.value);
});
fdata.append('answers[]', values);
http://jsfiddle.net/j5ezgxe9/

KnockoutJS, mapping plugin, get notified when changes in model?

Im using knockoutJS in following way:
var messagesFromServer = getJSONData().messages; //this get msgs from server
ko.mapping.fromJS(messagesFromServer, {}, myobject.viewModel.Messages);
Then i am basically calling this every three seconds to update html table, and it works just fine, new rows are added if new data found from server. Now i would like to add custom callback when something has actually changed, for example when new messages are found.
How should i implement this?
thanks in adv,
-hk
You could convert the two objects into json, then compare them json strings.
var messagesFromServer = getJSONData().messages; //this get msgs from server
var newString = ko.toJSON(messagesFromServer);
var oldString = ko.toJSON(myobject.viewModel.Messages);
if(newString != oldString ) {
// something new
}
ko.mapping.fromJS(messagesFromServer, {}, myobject.viewModel.Messages);
See ko.toJSON doc
I hope it helps.
If the messages is array, you can use ko.utils.compareArrays to detect the changes and raise custom events yourself. Here is code example for comparing ko.observableArray(). Look for Comparing two arrays

javascript array.push inside for loop--result not array?

I'm trying to populate an array by using a for loop to access certain records in a database and then combine the contents of some DB fields with text to create each array element. Here's what I have:
var numbers = [7,8];
var phsw = [];
for (var i=0,len=numbers.length;i<len;i++) {
selectObj = cObj.select().from("wp_posts").where('ID=?', numbers[i]);
result1 = cObj.exec(selectObj);
var resource = result1[0].guid;
var title = result1[0].post_title;
var tnTxt = result1[0].post_title;
var tn = resource.replace(/\.jpg/,"-150x150.jpg");
phsw.push({mytitle:'" + title + "', mythumbnail:'" + tn + "', mytntxt:'" + tnTxt + "', myresource:'" + resource +"'});
}
This creates what I thought was an array, except it's apparently not really an array. When I use console.log(phsw) to see what's in it I get this:
[{mytitle:'title', mythumbnail:'imagefile1tnlink', mytntxt:'thumbtxt1', myresource:'imagefile1link'},{mytitle:'title2', mythumbnail:'imagefile2tnlink', mytntxt:'thumbtxt2', myresource:'imagefile2link'}]
where I should get this:
[object Object]
if it was really an array (right??).
I'm using this in Application Craft, and the phsw array is being created with server side javascript and then being passed to a callback on the application side, but when it gets back to the app side, I can't use it to populate a widget because it's not really an array. How can I make sure phsw is an array?
And yes, I'm sure this code isn't the most clean or efficient way to do this; my (rather rusty) experience is in PHP and MySQL as opposed to javascript, so I'm always open to suggestions of better ways to do things!
No everything is correct. It is in fact an array containing exactly 2 objects.
Your expectation that you should get [object Object] is wrong.

SharePoint 2010 Client Object Model + set the value of a multiple lookup field with Javascript

I'm trying to update a list item with the SharePoint 2010 Client Object Model and Javascript. The item I am trying to update has a Multiple Value Lookup field. I can successfully set this field, but only with one value. Does anyone know how to set it with multiple values?
var _newLookupField = new SP.FieldLookupValue();
_newLookupField.set_lookupId(itemArray.toString()); //this works if array is only 1 item
currentItem.set_item('Lookup_x0020_Field', _newLookupField);
var lookupsIds = [1,2,4];
var lookups = [];
for (var ii in lookupsIds) {
var lookupValue = new SP.FieldLookupValue();
lookupValue.set_lookupId(lookupsIds[ii]);
lookups.push(lookupValue);
}
currentItem.set_item('Lookup_x0020_Field', lookups);
currentItem.update();
I have created a blog post that clearly explain how to add a list item that contains metadata with lookup fields.
http://vangalvenkat.blogspot.com/2011/10/adding-new-document-item-whose-metadata.html

Categories

Resources