I'm attempting this in coffeescript:
$( element ).mousedown( aFunction ).mouseup( anotherFunction );
I'm trying to work out a way to make use of indents so that something like the following will return what's about:
$ element
.mousedown aFunction
.mouseup anotherFunction
But to no avail, are there any recommendations for chaining in coffeescript?
I'm sure you don't want to use parenthesis, but...
$("#element")
.mousedown(aFunction)
.mouseup(anotherFunction)
Compiles to
$("#element").mousedown(aFunction).mouseup(anotherFunction);
For all the other quick readers out there, here's the updated answer by a paid nerd given here.
req = $.get('foo.html')
.success (response) ->
do_something()
.error (response) ->
do_something()
...compiles to:
var req;
req = $.get('foo.html').success(function(response) {
return do_something();
}).error(function(response) {
return do_something();
});
Looks like mus is too short suggested it in a comment above as well.
Related
I need to pass a callback to a function whose signature is function('ui', {foo: bar, callback: callbackfn}). The function I want to pass is a When.js promise.
The best I've come up with:
var d = when.defer();
var p = when(d);
var q = p.then(function() {
return loadItem(newCatalogItem, name, fileOrUrl);
});
ConfirmationMessage.open('ui', { callback: d.resolve });
return q;
This works (using a deferred to prevent immediate execution, and then passing the resolve function as the callback), but seems a bit convoluted.
Is there a cleaner way?
I think you want to just promisify that ConfirmationMessage.open method (see also the when.js docs here and there), and then use it like a promise function, chaining then calls onto it.
For your specific example, that could be (using the portable promise constructor):
return when.promise(function(resolve) {
ConfirmationMessage.open('ui', { callback: resolve });
}).then(function(confirmResult) {
return loadItem(newCatalogItem, name, fileOrUrl);
});
this is my zend controller
and this is how i called my service
please teach me how. it's important ! thank you so much
Seems like a bit of extra work with what you have there. I've done it this way in both ZF1 and ZF2:
PHP
// notice 'searchteams' is lowercased. There's been problems in the past
// when camelcasing action names, by default I believe ZF is looking for
// a lowercase action name unless you've configured it otherwise
public function searchteamsAction()
{
// make sure this is an ajax request (this is a method I usually write, if
// Zend has one you could use that here)
if ($this->isXmlHttpRequest())
{
// instantiate model, get the results, good
// disabling view and layout, good
// headers, never really had an issue that needed the headers to be set here
// would simply have..
echo json_encode($res);
}
}
JS: in the success method in the AJAX call, I'd do this:
success : function (data){
// parse the json
var parsed_data = $.parseJSON(data);
console.log(parsed_data);
// do stuff with parsed_data
}
Hope that helps.
Check your datas with a return var_dump($res); before $this->_response->setHeader(...
if you have datas, try to add $this->_response->setHeader('Content-Type', 'application/json', true);
If not work, try to replace
$this->_response->setHeader(...);
$this->_response->setHeader(...);
$this->_response->setHeader(...);
$this->_response->setHeader(...);
and echo Zend_Json::encode($res);
by only
return $this->_helper->json($res);
You can also see the error on the side of jQuery with something like:
error: function(xhr, status, error) {
alert("error status: " + status);
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
I hope it will help you :)
I'm about to set up a stupid play/scala app whose only job will be to make some http call once it receives calls itself
GET /abracadabra controllers.Application.abracadabra(stuff: String)
and then
def abracadabra(stuff: String) = Action {
Logger.info("called for stuff: "+stuff);
// call this other URL with 'stuff' as get parameter
// log http return status code and return Ok/200 anyways
}
Now for the second (commented) part I thought about using Dispatch.
I've read the docs but I can't just figure out how to use Promises and all that.
If anybody could point me to some sample code or something, it will be much appreciated
Since Play! has a built in Async library, you should probably go ahead and use that unless there's a feature in Dispatch that you specifically need.
Here's a short example:
def abracadabra(stuff: String) = Action {
Logger.info("called for stuff: "+stuff);
Async {
WS.url("http://stackoverflow.com/").get().map { response =>
Ok("I got it: " + response)
}
}
}
The documentation is here: https://github.com/playframework/Play20/wiki/ScalaWS
I'm using this tutorial to create a custom PhoneGap plugin:
http://wiki.phonegap.com/w/page/36753496/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20iOS
I have had success using the author's example, but I have a few questions that I have not been able to find out the answers to.
When the JavaScript function is created, the code is:
var MyPlugin = {
nativeFunction: function(types, success, fail) {
return PhoneGap.exec(success, fail, "PluginClass", "print", types);
}
};
Is there a way to set this up without var MyPlugin = {...}; and nativeFunction? In other words, can we define a function of our plugin like myfunc = function()...
Secondly, assuming there is a way to do the above, could this code:
MyPlugin.nativeFunction(
["HelloWorld"] ,
function(result) {
alert("Success : \r\n"+result);
},
function(error) {
alert("Error : \r\n"+error);
}
);
(which is the test code to test the plugin) also be written in a more standardized way? I.e., just a call to Javascript function without the nativeFunction part?
I would very much appreciate any input, thank you!
the phonegap documentation for plugins sucks. Honestly I had a bunch of issues when trying to create my own. A few tips :
the reason for doing
var MyPlugin = {};
is because this allows us to us scope things specific to that js object.
example:
MyPlugin.myFunction();
My favorite method to create plugins, similar to your question, is to prototype them
var MyPlugin = {}; // our object
MyPlugin.prototype.myFunction = function(success,fail,types){
}
The key to making a plugin fire is this -
PhoneGap.exec(success,fail,"MyPlugin","myFunction",types);
But something that they leave out is, what if we want to have options to our plugin? What if we want to do more than pass a string, then the example doesn't work. The fix is easy but not talked about at all.
var MyPlugin = {};
MyPlugin.prototype.myFunction = function(success,fail,options){
var defaults = {
foo: '', // these are options
bar: '',
};
// this parses our "options"
for(var key in defaults) {
if(typeof options[key] !== "undefined") defaults[key] = options[key];
}
return PhoneGap.exec(success,fail,"MyPlugin","myFunction",[defaults]);
}
when we call this with out javascript -
var foo = MyPlugin.myFunction(success,fail,{
foo:'hello',
bar:'world'
});
You'll notice that most of the phonegap API uses this syntax, which I found strange that their documentation didn't really talk about how to do this.
I have a post about a plugin I create you can check it out for reference.
Blog - http://www.drewdahlman.com/meusLabs/?p=138
Git - https://github.com/DrewDahlman/ImageFilter
I'm using Jasmine with CoffeeScript (in Rails 3.1) and test_track. Everything else is fine, but I cant have spyOn on function calls to work:
describe "spyOn", ->
it "should spy", ->
foo =
bar: ->
spyOn(foo, 'bar')
foo.bar()
expect(foo.bar).toHaveBeenCalled()
the expect fails. What's wrong?
Update: the same exact code works on tryjasmine. It could be a bug with the version of jasmine with test_track.
For those who got the same problem, here's the answer:
https://github.com/quickleft/test_track/issues/7#issuecomment-2570580
Have a look at the compiled JavaScript and debug from there. Perhaps those returns are causing problems?
describe("spyOn", function() {
return it("should spy", function() {
var foo;
foo = {
bar: function() {}
};
spyOn(foo, 'bar');
foo.bar();
return expect(foo.bar).toHaveBeenCalled();
});
});