Search code examples
jquerywordpressbackbone.jsmarionettegravity-forms-plugin

How to call a function included in an index.html in a Backbone Marionette Collection


Very newbie question:

I follow David Sulc book Backbone.Marionette.js: A Serious Progression and try to learn Backbone and Marionette. I want to use WordPress Gravity Forms Web API as a RestAPI server since I do not know any server side language. In order to Authenticated, I need to include this JavaScript file to generate the rootUrl in the model and Url in the collection: (http://www.gravityhelp.com/documentation/page/Web_API) The JavaScript portion.

So I need to include 2 script files in the index.html(located in the root folder) before like this:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>

Then in the collection(Located in js/entities/contact.js), I need to do this:

ContactManager.module("Entities", function(Entities, ContactManager, Backbone, Marionette, $, _){
Entities.ContactCollection = Backbone.Collection.extend({

    calculateSig: function(stringToSign, privateKey){
        var hash = CryptoJS.HmacSHA1(stringToSign, privateKey);
        var base64 = hash.toString(CryptoJS.enc.Base64);
        return encodeURIComponent(base64);
    },

    url: function(){
        var d = new Date,
         expiration = 3600 // 1 hour,
         unixtime = parseInt(d.getTime() / 1000),
         future_unixtime = unixtime + expiration,
         publicKey = "1234",
         privateKey = "abcd",
         method = "GET",
         route = "forms/1/entries";

        stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime;
        sig = this.calculateSig(stringToSign, privateKey);
        console.log(sig);

        return sig;
    },

    model: Entities.Contact

});
});

The first stupid question I have is how I can call the "CryptoJS.HmacSHA1" function and "CryptoJS.enc.Base64" within the calculateSig which within the Backbone Collection... If I use my namespace ContactManager.CryptoJS.HmacSHA1, it still says Cannot read property 'HmacSHA1' of undefined.

No idea how to implement Gravity Form authorization script to generate the url for backbone. Please help!


Solution

  • The problem is my site is under SSL / HTTPS and I source the CryptoJS file with http. So my model / collection never get the global var.