0

Im fairly new to backbone and running into a weird problem

I have a model that looks like so:

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

var Backbone = require( 'backbone' );

return Backbone.Model.extend(
    {

        defaults: {
            isLoggedin: false
        },

        url: 'http://api.com/login',

        parse: function( data ){
            //do stuff based on call

        },
        initialize: function(){


        },
        doLogin: function( data ){

            this.fetch({
                data: data,
                type: 'POST'
            });

            this.isUserLoggedIn();
        },

        isUserLoggedIn: function(){

            this.fetch({
                url:'http://api.com/loggedIn',
                type: 'GET'
            });
        }

       doLogout: function () {

            this.fetch({
                url:'http://api.com/logout',
                type: 'POST'

                });


         }

    });
} );

As you can see i have 3 different functions happening what i need to do is based on the function i need to do different actions or return different results but with having one parse function how can i know which function is being called to do whatever i need in the parse. For example if isUserLoggedIn function is called i need to redirect them to some page based on response if doLogout is called i need to redirect them to another page based on response. Any ideas?

Yeak
  • 2,470
  • 9
  • 45
  • 71
  • You can set another attr state to remember what's the last called function, or you can let the api to return some value to tell you which function call it. – fuyushimoya Sep 24 '15 at 17:36
  • Can you please tell me how to do that as i mentioned im new to backbone and js – Yeak Sep 24 '15 at 17:42

2 Answers2

1

You need to store the last called function like this:

return Backbone.Model.extend(
{

    defaults: {
        isLoggedin: false
    },

    url: 'http://api.com/login',

    parse: function( data ){
        if(this.lastCall == 'doLogin'){
            //do staff if doLogin was called
            this.isUserLoggedIn();
        }
        if(this.lastCall == 'isUserLoggedIn'){
            //do staff if isUserLoggedIn was called
        }
        if(this.lastCall == 'doLogout'){
            //do staff if doLogout was called
        }

    },
    initialize: function(){


    },
    doLogin: function( data ){
        this.lastCall = 'doLogin';
        this.fetch({
            data: data,
            type: 'POST'
        });


    },

    isUserLoggedIn: function(){
        this.lastCall = 'isUserLoggedIn';
        this.fetch({
            url:'http://api.com/loggedIn',
            type: 'GET'
        });
    }

   doLogout: function () {
        this.lastCall = 'doLogout';
        this.fetch({
            url:'http://api.com/logout',
            type: 'POST'

            });


     }

});
MysterX
  • 2,318
  • 1
  • 12
  • 11
0

You might want to use second argument of the parse, that contain options.

MY_CONST = {
  SOME_KIND_OF_URL: '/api/prod'
}

if (dev) {
  MY_CONST.SOME_KIND_OF_URL = '/api/foo';
}

MyModel = Bacbone.Model.extend({
  parse: function(response, options) {
    if (options.url === MY_CONST.SOME_KIND_OF_URL) {
  return 'foo';
    }
  }
},  MY_CONST)
Lesha Ogonkov
  • 1,218
  • 8
  • 20
  • If i use options i get the url of which call was made however i will have problems when i have a dev and production version of the api call. I know i can regex it and get the last parameter but that just seems too dirty – Yeak Sep 24 '15 at 17:54
  • You can use some kind of constants, that override for prod/dev. And use them in your parse method aswell. – Lesha Ogonkov Sep 25 '15 at 05:05