/** * Created by Denis on 19/06/2014. */ define(['jquery', 'backbone'], function ($, Backbone) { var Profile = { _loadme: function () { if (this.isConnected()) { if (this.data.type == null) this.loadProfile(); } return this; }, dataloaded: false, data: { "name": null, "email": null, "gender": null, "tema": 'b', "risRic": '10', "type": null }, isConnected: function () { return this.getKeySave() != "null" && this.getKeySave() != null && this.getKeySave() != "undefined" && this.getKeySave() != undefined; }, loadProfile: function (successFn) { this.internalLoadProfile(this.getKeySave(), successFn); }, internalLoadProfile: function (keyStore, successFn) { if (this.dataloaded) return; //var keyStore = this.getKeySave(); var self = this; var retBool = false; var resp = $.ajax({ url: myManifest.Settings.DefaultURL + "/api/profile/" + keyStore, //dataType: "json", //async: false, type: 'GET', success: function (response) { if (response.length > 0) { self.dataloaded = true; self.setKeySave(keyStore); self.data.name = response[0].Name; self.data.gender = response[0].Gender; self.data.tema = response[0].TemaUI; self.data.risRic = response[0].RisultatiRicerca; self.data.type = response[0].TipoAccesso; retBool = true; } if (successFn) successFn(retBool); } }); }, getKeySave: function () { return window.localStorage.getItem("KeySave"); }, setKeySave: function (val) { window.localStorage.setItem("KeySave", val); }, isGPlusConnected: function () { if (this.data.type === "google") return true; return false; }, isFbConnected: function () { if (this.data.type === "facebook") return true; return false; }, addRicettaBloccoNote: function (ricetta_id, successFn) { var sendobj = { "ricetta_id": ricetta_id, "keyStore": this.getKeySave() }; $.ajax({ url: myManifest.Settings.DefaultURL + "/api/profile/ricetta", //dataType: "json", type: 'POST', data: {dataPair: JSON.stringify(sendobj)}, success: function (msg) { if (successFn) successFn(msg); } }); }, removeRicettaBloccoNote: function (ricetta_id, successFn) { $.ajax({ url: myManifest.Settings.DefaultURL + "/api/profile/" + this.getKeySave() + "/ricetta/" + ricetta_id, //dataType: "json", type: 'DELETE', success: function (msg) { if (successFn) successFn(msg); } }); }, existRicettaBloccoNote: function (ricettaID, successFn) { $.ajax({ url: myManifest.Settings.DefaultURL + "/api/profile/" + this.getKeySave() + "/ricetta/" + ricettaID, //dataType: "json", type: 'GET', success: function (msg) { if (successFn) successFn(msg); } }); }, createProfile: function (keyStore, typeProfile, name, email, gender, successFn) { this.data.type = typeProfile; this.data.name = name; this.data.email = email; this.data.gender = gender; this.setKeySave(keyStore); window.localStorage.removeItem("risRic"); window.localStorage.removeItem("tema"); var sendobj = { "keyStore": this.getKeySave(), "name": this.data.name, "email": this.data.email, "gender": this.data.gender, "tema": this.data.tema, "risRic": this.data.risRic, "type": this.data.type }; $.ajax({ url: myManifest.Settings.DefaultURL + "/api/profile", //dataType: "json", type: 'POST', data: {dataPair: JSON.stringify(sendobj)}, success: function (msg) { if (successFn) successFn(msg); } }); }, updateProfile: function (successFn) { if (this.isConnected()) { var sendobj = { "keyStore": this.getKeySave(), "tema": this.data.tema, "risRic": this.data.risRic, "type": this.data.type }; $.ajax({ url: myManifest.Settings.DefaultURL + "/api/profile", //dataType: "json", type: 'PUT', data: {dataPair: JSON.stringify(sendobj)}, success: function (msg) { if (successFn) successFn(msg); } }); } else { window.localStorage.setItem("risRic", this.data.risRic); window.localStorage.setItem("tema", this.data.tema); } } }; return Profile._loadme(); });