SuperPlan(13)Winner Seller Server - JSONP Basic Auth

SuperPlan(13)Winner Seller Server - JSONP Basic Auth

15. Basic Auth on JSONP Server
If we are using the basic auth, we can do the jsonp client like this.

Here is the View Controller of Backbone.js
…snip...
    render: function(){
      window.logger.debug("I am going to hit the Nav Bar Template Page.");
      var widget = this;

      var navBars = new NavBarsModel();

      window.logger.debug(" navBars url = " + navBars.url);
      navBars.fetch({
         data: {},
         type: 'GET',
         success: function(data, response, options) {
             window.logger.debug("NavBarView data = " + data);
             var compiledTemplate = _.template( htmlTemplate, { items : data } );
             $("#navBar").html(compiledTemplate);
         },
         error: function(jqXHR, textStatus, errorThrown) {
            window.logger.error('error arguments: ', arguments);
            window.logger.error(jqXHR + " " + textStatus + " " + errorThrown);
         },
         complete: function(xhr, textStatus) {
            window.logger.debug(textStatus);
         }
      });
    }
…snip…

Here is the Model Layer of Backbone.js

…snip...
    var Items = Backbone.Collection.extend({
        url: '/navbars',
        parse: function(response) {
            window.logger.debug("getting NavBars from response=" + response);
            return response;
        },
        sync: function(method, model, options){
           options.timeout = 10000;
           //json mock
           //options.url = 'http://localhost:9000/data' + "/navbars" + ".JSON";
           //options.dataType = 'json';

           //jsonp
           options.dataType = "jsonp";
           options.crossDomain = true;
           options.url = 'http://' + 'customer' + ':' + 'customer' + '@' + 'localhost:9002/v1/sillycat' + '/navbars';

           return Backbone.sync(method, model, options);
        }
    });
…snip…

It the server side, we also use jsonpwithParameter
…snip...
    pathPrefix(Version / BrandCode) { (apiVersion, brandCode) =>
      authenticate(BasicAuth(new BrandUserPassAuthenticator(dao), "Realm")) { user =>
        path("products") {
          jsonpWithParameter("callback") {
            complete(HttpBody(`application/json`,
              dao.db.withSession {
                logger.debug("Hitting the URI navbars with apiVersion=" + apiVersion + ",brandCode=" + brandCode)
                DefaultJsonProtocol.listFormat[NavBar].write(dao.NavBars.all).toString
              }
            ))
          }
        }
      }
    }
…snip…

It is not nice and pretty to use POST in jsonp. So I will change back to JSON for both the server side and client side.

16. JSON cross domain
come soon...

17. Integration(Backbone + Require + Jasmine + Phantom + Grunt + Bootstrap)
come soon…


References:
integration
http://hdnrnzk.me/2013/01/10/backbone-requirejs-jasmine-phantomjs-and-grunt/
https://github.com/ghiden/backbone-requirejs-jasmine-phantomjs-grunt-setup

superplan 12
http://sillycat.iteye.com/blog/1881952



猜你喜欢

转载自sillycat.iteye.com/blog/1885313
今日推荐