0

Very interesting situation!

The code below represents order entry. Before saving User should calc order Total what he does by clicking on #calcTotal button and then he clicks on #saveOrder button.

The problem is that if User launches saveOrder function just for one time then ajax method in calcTotal function will always call for model.save in saveOrder function!

I think this.model.save function override some Backbone model functions so any ajax request will execute any actions in model.save.

How I should prevent this behaviour?

events: {
     'click #calcTotal': 'calcTotal',
     'click #saveOrder': 'saveOrder'
},
calcTotal: function() {
    this.model.set( $('form').serializeJSON() );
    var that = this;

    $.ajax({
        url: 'api/order/calcTotal',
        type: 'GET',
        data: this.model.toJSON(),
        dataType: 'json',
        success: function( result ) {
            that.model.set({
                'total': result.total
            });
            that.renderTotal();
        }
    });
},

saveOrder: function() {
    var that = this;

    this.model.save({
        success: function() {
            console.log('CHECK');
        }
    });
},
Kamil Davudov
  • 363
  • 1
  • 3
  • 15
  • If I understand what you're saying correctly, (your question is confusing) you've probably bound model.save() to your model's set event somewhere. Also, [you shouldn't AJAX back to your server just to calculate a total](http://stackoverflow.com/questions/25924499/dry-when-validating-calculations-made-client-side-javascript-on-the-server-php). You should write that client side. It will be much faster and result in a better user experience. – Michael.Lumley Apr 29 '15 at 16:05
  • @Morslamina, i don't want to calculate that client side because i have some discount configs in database that Admin can change so total counting algorythm can be changed. – Kamil Davudov Apr 29 '15 at 16:08
  • Then you can load those configs via AJAX separately, long before your user ever clicks `#calcTotal`, store them client side, and then use them for calculation when you need them. In fact, you can set it up so that backbone automatically runs your total for you without even needing a click on `#calcTotal`. It will result in a much better UI, IMHO. – Michael.Lumley Apr 29 '15 at 16:10
  • @Morslamina, you are correct in general. But you give user opportunity for XSS attack. he can change these configs and save incorrect data. I created a function on server side where I calculate total via AJAX and one more time also when user saves an order. – Kamil Davudov Apr 29 '15 at 16:13
  • Read the link I posted in my first comment. You will not be vulnerable to XSS attack because you will validate the submission when it comes into your server. Calculate client side for speed/UI experience, then validate server side for security. – Michael.Lumley Apr 29 '15 at 16:16
  • @Morslamina, I understand you. I just didnt to write two times same function (calculation and validation) xD – Kamil Davudov Apr 29 '15 at 16:18
  • As discussed at the link, better practice is to write it twice, or to use node.js to use your same validation method server side. But I'm a lazy guy too, so I feel your pain. ;) – Michael.Lumley Apr 29 '15 at 16:21
  • well the problem seems odd , probably some other part of code must be causing issue. Create a fiddle to find the issue – StateLess Apr 30 '15 at 05:35

0 Answers0