I've inherited a backbone js based app. I really like backbone and i'm just starting to get my head around it. From my understanding when model.save is called on a new entity it should post that to the server, the server should return the same json but with an id alloted and backbone should persist that id to the model so that further saves result in a PUT with the ID for update.
However, when I call model.save() and then try to get the model.id property, it's null.
Is this because I'm not doing it with a call back? So the property hasn't been set yet?
How would I set the success callback? calling model.save({success: function(){...}})doesn't work?
here is the actual call:
model.save(null, {
success: function () {
alert('success');
},
error: function () {
alert('error');
}
});
Something feels odd about this. Setting silent: true only makes it so none of the events get fired. Everything else should happen normally. In other words, don't assume that setting slient: true is the right answer here...
I suspect you are actually throwing an exception some place (probably with validation or something like that) and somehow, setting silent: true is causing everything to flow through.
I would strongly suggest that you remove this option and check your console or run with the debugger... I suspect you have a bug lurking around there some place.
Some suggestions: Take a look at the annotated source for the model.set function. It gets called before your success callback will get called. Inside of that function, there are several things that will happen if silent is false. These include validation, individual property change triggers, and a global change trigger. I would bet money that either the validation is failing or something that is listening to the changes is throwing an exception.
i needed to set the silent: true on the save:
model.save(null, {
silent: true,
success: function () {
alert('success');
},
error: function () {
alert('error');
}
});
I had the same issue, turned out to be because my custom parse was failing
Related
I am listening to websocket events with SockJS and want to insert received objects into my $scope.mails.items which is an array. I have the below code snippet and my problem is that for some reason I am not able to pass the message into my delayed function. I know... I tried to read some explanations about this issue asked repeatedly, but still was not able to figure out why it is not working in this particular case. The reason I need to delay this is that I'd like to make sure it gets applied to my view, it does not otherwise.
MyService.receive().then(null, null, function(message) {
$timeout(function(m) {
if($scope.mails.items.indexOf(m) == -1) {
$scope.mails.items.push(m);
}
}, 0, true, message);
});
When debugging it, I can see that the message variable has proper value but when it comes to stopping in the middle of my delayed function, m is not getting the data, however I would expect $timeout to pass it down.
Can you please help?
Not sure why m is not getting value (explanation welcome), but this works:
MyService.receive().then(null, null, function(message) {
$timeout(function() {
if($scope.mails.items.indexOf(message) == -1) {
$scope.mails.items.push(message);
}
}, 0, true, message);
});
The callback function for $timeout does not take any parameters. That m parameter is always going to be null, just use the message parameter from the outer function.
I'm trying to craft a simple way to defer subsequent JQuery/JavaScript code execution until after this statement is performed and the variable cache[url] has the object returned to it by the .load() operator:
cache[url] = $('<div class="bbq-item"/>').appendTo('.bbq-content').load(url);
This statement occurs in the middle of a hashchange listener function:
$(window).on( 'hashchange', function(e) {
etc.
...and I cannot move the code dependent on the success of the .load() outside of it.
It does not rely on external PHP, JSON, or anything that the typical AJAX "deferred" and "when" operators seem to thrive upon in the examples I've found online; this is pure DOM interrogation and manipulation via Javascript/JQuery.
I've tried wrapping the code that needs to follow it (and is dependent on its success) by wrapping it in a simple "if" clause, like this:
if (cache[url] = $('<div class="bbq-item"/>').appendTo('.bbq-content').load(url)) {
[...code that is dependent on success of the .load()...]
}
...but that doesn't always work, as the loading takes longer than the evaluation in some cases, it seems.
What would be the best strategy to accomplish this?
According to the documentation located here (http://api.jquery.com/load/), you can pass a callback function .load( url [, data ] [, complete ] ) into load and then call it so it would like this:
$('<div class="bbq-item"/>').appendTo('.bbq-content').load(url, function(responseText, textstatus){
cache[url] = responseText;
if(textStatus === "success" || textStatus === "notmodified"){
[...code that is dependent on success of the .load()...]
}
});
Edit: this is the closest you can get since load always returns something into cache[url]... otherwise you won't know if its successful or not.
As a note: you'll always get the if test passing because setting a variable will always pass truthy to the conditional so doing if(cache[url] = $.load()) will always evaluate
I know that I can subscribe to events using Wicket 6+ which is what I do in my application. Now I am trying to cancel an Ajax event on a particular occurrence of some condition like that:
Wicket.Event.subscribe('/ajax/call/before', function (jqEvent,
attributes,
jqXHR,
errorThrown,
textStatus) {
if(someCondition) {
// Abort event, but how?
}
});
I am looking for a way to abort the event, but the normal jQuery event handlers
jqEvent.stopImmediatePropagation();
jqEvent.preventDefault();
or even
attributes.event.stopImmediatePropagation();
attributes.event.preventDefault();
but they do not seem to work. If the method returns a value, this does not seem to have an effect either. The easiest I found so far is simply throwing an exception but this solution is far from clean.
It is rather hacky but by following down the stack I found a solution to this. Wicket treats the component's callback handlers privileged. If you are registering an IAjaxCallListener, it will be allowed to return false which is read by Wicket and what stops the propagation. The "global" event listeners are for some reason denied this privilege which is why the return value must be smuggeled into an not-yet existing array of precondition handlers. It's messy but it works.
Wicket.Event.subscribe('/ajax/call/before', function (jqEvent,
attributes,
jqXHR,
errorThrown,
textStatus) {
if (precondition) {
attributes.pre = [stopWicket];
}
}
});
function stopWicket() {
return false;
}
Anyways. who calls something a "precondition" but ignores the decision made on this precondition handler....
A subscriber cannot stop Ajax processing.
Why not use a precondition for this? See AjaxRequestAttributes#getAjaxCallListeners() and IAjaxCallListener#getPrecondition().
Pjax will not allow me to specify functions as an option when calling it directly.
$(document).on('click', 'a.inferno-preview', function(event) {
return $.pjax.click(event, {
container: '#preview-overlay',
fragment: '#preview-overlay',
send: function() {
return $('#preview-overlay').removeClass('hidden');
},
complete: function() {}
});
});
In this case, the 'send' and 'complete' functions are not being executed, while 'container' and 'fragment' is working well. Why is this and what do I need to do to make Pjax recognize my functions?
By the way: I can not use the conventional form of using Pjax, I need more control about the happening, so I need to use the $.pjax.click object. Yet it would work fine with the functions in the common way like in the following:
$(document).pjax('a.inferno-preview', '#preview-overlay', {
send: function() { alert('this will work.'); }
});
In pjax send and complete events are not called if a request if retrieved from cache so you will not get your overlay down. Instead use success or complete depending on your particular use case.
I'm just playing around for the first time with jQuery's ajax functionality. I wanted to add a function that could handle any errors. So, in one of my client javascript blocks, I added the following line:
<script type="text/javascript">
....
$.ajax({ error: function () { alert('boo'); } })
....
</script>
I expected that this would bind the error handler, so that when an error occurs, it would fire the anonymous function included.
What happens instead though, is that it immediately fires the function on page load, as soon as it parses this line of code.
What am I doing wrong? What is the proper way to bind the ajax error handler?
I'm not sure if I understood your question correctly, let me know if I've misunderstood.
I assume that you are trying to create a generic ajax call error handler? If that's the case, you have got the wrong idea.
Are you are just trying to bind the event handler? In this case, you are executing it.
I would recommend you read and check out the examples on these jQuery API reference docs:
API/1.3/Events
Ajax/jQuery.ajax
Also check out the post link provided by F.Aquino and this SO post: JavaScript Exception Handling.
This is could be helpful too: Handling AJAX Errors With jQuery.
You want to change the global settings. Check jQuery documentation.
$.ajaxSetup({
error: function () { alert('boo'); }
});