vendredi 27 février 2015

Send data with ajax, return json


I want to send a string and return json.


This works but it doesn't return json code



$.ajax({
url: "getFeed.php",
type: "post",
data: myString
});


When I want it to return a json string, it gives me an error.



$.ajax({
url: 'getFeed.php',
type: "post",
data: {string1: "testdata", string2: "testdata"},
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
});


For some reason the server doesn't recieve any data.


How am I supposed to write this? (i want to send 2 strings)





Cart not being updated after clicking link through JS


If anyone can provide assistance on my code that would be great. I am currently using WooCommerce and have put all the 'add-to-cart' links into a separate mySQL database. For example, this link: http://ift.tt/1DiVRaW adds a product to the cart.


I have created a web application with the separate mySQL database that the user can use to add products to the cart. My problem is that the cart does not get updated when he/she clicks the link.



<?php
$producturl = ProductURL::find(array('Product'=>$tube1->Tube1));
if($producturl[0]->URL!=NULL) {
echo '<span id="shop">Test2 Cart</span>';
}
?>

<script type="text/javascript">
$(document).on('click', '#shop', function (event) {
$.post( '" . $producturl[0]->URL . "' );
event.stopPropagation();
event.preventDefault();
alert('Product has been added to cart');
});
</script>


$producturl[0]->URL is the table that pulls my links. Is $.post not the right method for the server to recognize the user clicking the link? Help would be appreciated, thanks!





Clicking a checkbox using protractor


There is no unique model, id, label names. How am I suppose to check/click the checkboxes?


The html/code is:



<div ng-repeat="column in $parent.columns.standardMetrics" class="ng-scope">
<label class="ng-binding">
<input type="checkbox" ng-model="column.visible" name="" class="ng-pristine ng-valid">
uCPM
</label>
</div>

</div><div ng-repeat="column in $parent.columns.standardMetrics" class="ng-scope">
<label class="ng-binding">
<input type="checkbox" ng-model="column.visible" name="" class="ng-pristine ng-valid">
CPM
</label>
</div>


There is about 15 of these with the same name besides the label next to the name. I searched everywhere to see if protractor allows you to find elements by label.


My attempts are finding via by.buttonText, by.partialButtonText, by.css and by.binding. I have no luck on this.


My purpose is to pass in a label name that I can toggle the specific checkbox. So it's like element(by.css(header)).click(); where header is what is passed in.





The gzuncompress of php equivalent on javascript


I have a php file that do:



echo(gzcompress("TEST COMPRESS"));


and on javascript i do a request on php and i want a function that do the same of gzuncompress(data) for take the result = "TEST COMPRESS" again on client side.


EDIT: If someone ready this question, the zlib will works if the php is with gzencode(), gzcompress will not work.





ExtJS -- highlight matching nodes for tree typeAhead


I'm using a tree filter plugin created by one of the members at Sencha. Here is his fiddle for the plugin: http://ift.tt/1ASeLqS


As you can see in the fiddle when a typeAhead result is a parent node, all of its children are expanded. So I want to highlight all matching results because I often get numerous parent nodes that contain the matching typeAhead search term and they have hundreds of children, making it hard to find the matching parent node(s).


copy pasting a snippet from the fiddle since SO is throwing "links to jsfiddle must be accompanied by code" error...



filter: function (value, property, re) {
var me = this
, tree = me.tree
, matches = [] // array of nodes matching the search criteria
, root = tree.getRootNode() // root node of the tree
, property = property || 'text' // property is optional - will be set to the 'text' propert of the treeStore record by default
, re = re || new RegExp(value, "ig") // the regExp could be modified to allow for case-sensitive, starts with, etc.
, visibleNodes = [] // array of nodes matching the search criteria + each parent non-leaf node up to root
, viewNode;

if (Ext.isEmpty(value)) { // if the search field is empty
me.clearFilter();
return;
}

tree.expandAll(); // expand all nodes for the the following iterative routines

// iterate over all nodes in the tree in order to evalute them against the search criteria
root.cascadeBy(function (node) {
if (node.get(property).match(re)) { // if the node matches the search criteria and is a leaf (could be modified to searh non-leaf nodes)
matches.push(node) // add the node to the matches array
}
});

if (me.allowParentFolders === false) { // if me.allowParentFolders is false (default) then remove any non-leaf nodes from the regex match
Ext.each(matches, function (match) {
if (!match.isLeaf()) { Ext.Array.remove(matches, match); }
});
}

Ext.each(matches, function (item, i, arr) { // loop through all matching leaf nodes
root.cascadeBy(function (node) { // find each parent node containing the node from the matches array
if (node.contains(item) == true) {
visibleNodes.push(node) // if it's an ancestor of the evaluated node add it to the visibleNodes array
}
});
if (me.allowParentFolders === true && !item.isLeaf()) { // if me.allowParentFolders is true and the item is a non-leaf item
item.cascadeBy(function (node) { // iterate over its children and set them as visible
visibleNodes.push(node)
});
}
visibleNodes.push(item) // also add the evaluated node itself to the visibleNodes array
});

root.cascadeBy(function (node) { // finally loop to hide/show each node
viewNode = Ext.fly(tree.getView().getNode(node)); // get the dom element assocaited with each node
if (viewNode) { // the first one is undefined ? escape it with a conditional
viewNode.setVisibilityMode(Ext.Element.DISPLAY); // set the visibility mode of the dom node to display (vs offsets)
viewNode.setVisible(Ext.Array.contains(visibleNodes, node));
}
});
}


Thanks.





Multiple mouseover events per element [duplicate]



This question already has an answer here:




I am visualizing a chat conversation where I append a bar per message representing message length. There is also another part of the interface for displaying statistics for each user in the chat.


GOAL: When a user hovers over a bar



  1. highlight the bar (by changing from grey to some other color)

  2. display data about the user who sent that message


So I wanted to do two mouseover events, one to highlight bar and the other to change the display, but it seems like only one mouseover event can be attached per element. How would I make both events fire?



// add highlighting event to each rectangle
rects.on('mouseover', function(thisData) {
rects.filter(function(d) { return d['userId'] === thisData['userId']; })
.style('fill', users[thisData['userId']]['color']);
});

// further down...

// change display when highlighting a rectangle
rects.on('mouseover', function(thisData) {
display.text(thisData['message']); // just example code
});




Get all checked checkboxes values


i have the following form:



<%= form_for([company,service], :remote => true) do |f| %>
<%= health_care_insurances.each do |hc_insurance| %>
<label><%= hc_insurance.name %></label>
<input id="health_care_insurance_<%= user.id %>" name="<%= hc_insurance.id %>" value="<%= hc_insurance.id %>" type="checkbox"><br/>
<% end %>
...
<% end %>


When it's submited, and for the sake of good performance, i would like to get an array with all "hc_insurance.ids" that are checked. I know i might use javascript to add them to a hidden_field and then pick them up from there, but im not sure that's a good way to go.


Is there any rails way to pick them up from the controller?