0

I have this save method.

  $scope.save = function(){
    $scope.product.$save(function(pipe){ 
      alert("fire!"); //not working
    });
  };

It works. It save the post to the database. But I want to display a message for the users. Something like "Your update has been saved". So obviously the callback method is suited for this. But it's not working. I get no alert("fire") when I run this. Even though the $save is successful. What can be wrong? How can I debug this?

PS. It's hard do make a fiddle/plunker out of this. But leave comment if I need to include any more code.

Joe
  • 4,274
  • 32
  • 95
  • 175

4 Answers4

1

Problem solved.

The issue was that the server (node js) sent an incomplete response.

I only had this:

connection.query("UPDATE product SET ?  WHERE id = ?"

I changed it to this:

  if (connection) {
    connection.query("UPDATE product SET ?  WHERE id = ?",  post, function(err, rows, fields) {
      if (err) throw err;
      res.contentType('application/json');
      res.write(JSON.stringify(rows));
      res.end();
    });
  }

Then the callback worked as expected.

Joe
  • 4,274
  • 32
  • 95
  • 175
0

Property Evaluation

Evaluation of all properties takes place against a scope. Unlike JavaScript, where names default to global window properties, Angular expressions have to use $window to refer to the global window object. For example, if you want to call alert(), which is defined on window, in an expression you must use $window.alert(). This is done intentionally to prevent accidental access to the global state (a common source of subtle bugs).

Inject the $window module into your Controller first and then do ..

$window.alert('fire!');
dcodesmith
  • 9,590
  • 4
  • 36
  • 40
0

May be the promise isn't ready when you call the alert, using .then() and .catch() could be the solution. See this too Promise on AngularJS resource save action

Community
  • 1
  • 1
ezeed
  • 58
  • 1
  • 6
0

You might be having some error message at the console of the browser, an incorrect response from the server could throw an exception if it cannot be parse correctly your response from server, that btw should be the updated resource (json) or blank.

Check also this question that might be related

Community
  • 1
  • 1
dimirc
  • 6,347
  • 3
  • 23
  • 34
  • If there was an incorrect response from server, then the database would not be updated. But it is getting updated and it's also reflected in my view. – Joe Jan 31 '14 at 13:39
  • The serve could be saving correctly but that doesn't mean that you are sending the "expected" data that is this case is a json with of the updated resource (product). Did you check the console at browser? Any message? – dimirc Jan 31 '14 at 13:45