0

I have a view.js and a model called user.js

user.js :

define( function ( require, exports, module )
{
    "use strict";

    var Backbone = require( 'backbone' );

    return Backbone.Model.extend({
        defaults: {
            flush: false
        },

        doRejectBatch: function( batchId, flushToConveyer ){
            this.functionName = 'doRejectBatch';
            this.flush = flushToConveyer;

            this.fetch({
                type: 'GET',
                url:'http://api.com/reject/' + id +'/rej',
                xhrFields: {
                    withCredentials: true
                }
            });
        },

        doPatchBatch: function( batchuuid ){
            var patchData = {
                "status": "rejected",
                "jobs_to_conveyer": this.flush
            };

            this.fetch({
                type: 'PATCH',

                url: 'http://api.com/api/rej/' + uuid,
                contentType: 'application/json',
                data: JSON.stringify(patchData),
                xhrFields: {
                    withCredentials: true
                }
            })
        }
    });
} );

When a function is complete the onSync function in my view.js is called.

 onSync: function ( rawData, response ){

        var msg = "Reject Request: successfull";

        app.regionMessage.show(new  MessageView({
            model: new Backbone.Model({
                msg_type: response[0].status,
                msg: msg
            })
        }));

    }

});

How can I determine in the view.js onSync function which functions response is sent from the model? In other words was the doRejectBatch function used or is the response from the doPatchBatch function? Note that the view.js is actually the controller and not really a view

Yeak
  • 2,470
  • 9
  • 45
  • 71

2 Answers2

1

In an MVC world, your view shouldn't really know anything about your model: your controller should take care of mapping Model to View, and vice versa. In practice, that can be difficult to enforce without a more stringent framework model than Backbone provides, but if you decouple these concepts now, you'll be happier in the long run.

So, let's rephrase your question. Instead of:

How can I determine in the view.js onSync function which functions response is sent from the model? In other words was the doRejectBatch function used or is the response from the doPatchBatch function?

phrase it something like:

How can I determine the correct message to show during onSync? In an MVC world, you'd do something like:

  • Model: Calls doRejectBatch or doPatchBatch. Inside these methods, set a status or state flag.
  • Controller: Binds to the model's sync event, or even on just the property it cares about (status or state)
  • Controller: In the Controller's onSync handler (not the view's), render an updated view with the appropriate state (e.g., calls a displayStatusMessage method or similar, with only the information needed to display the message. Depending on your requirements, that may not be the entire model or response object; it may simply be a status indicator.)

You may also wish to explore alternative designs like MVP or MVVM. Depending on your application's requirements and complexity, one of those patterns may work better for you than traditional MVC.

Palpatim
  • 9,074
  • 35
  • 43
  • I do agree that the model should be handling everything unfortunately the structure for this app is already set up and i cannot change it. In this case the view.js is actually acting like the controller for that page. So in my model i have functions that are called and the response is automatically sent to the onsync function in the view.js (which is technically the controller) but i have no way of knowing which response or which function is sending the response to know how to handle the response accordingly – Yeak Oct 01 '15 at 18:09
0

You could trigger a different event from each of the functions in the model, e.g:this.trigger('reject:batch') and this.trigger('patch:batch'). You'd then have a different handler function in your view, e.g

this.model.on('reject:batch', function () { /* specific handler code for batch reject here */);

Iain McNaught
  • 48
  • 1
  • 1
  • 6