Created
August 27, 2013 15:48
-
-
Save freidamachoi/6355328 to your computer and use it in GitHub Desktop.
ngForceService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
ot.service('myService', function myService (localStorage, $rootScope, $q, vfr) { | |
var self = this; | |
self.add = function(item, premise) { | |
if(!self.premise || !self.premise.id) { | |
self.premise = { | |
id: premise.Premise__c, | |
description: premise.Premise__r.StreetAddress__c | |
}; | |
} | |
if (self.premise.id === premise.Premise__c) { | |
if (item) { | |
item = angular.copy(item); | |
self.items.push(item); | |
} | |
} else { | |
alert('Not possible to add tenants from separate locations.'); | |
} | |
}; | |
self.remove = function(item) { | |
var index = self.items.indexOf(item); | |
if (index >= 0) { | |
self.items.splice(index, 1); | |
} | |
if (!self.items.length) { | |
self.premise = {}; | |
} | |
}; | |
self.reset = function() { | |
self.items = []; | |
self.premise = {}; | |
}; | |
self.submitLead = function() { | |
var myServiceRecordIds = [], | |
leads = {}; | |
if (self.items.length) { | |
myServiceRecordIds = _.pluck(self.items, "Suspect__c"); | |
myServiceRecordIds = _.map(myServiceRecordIds, function(id){ return "'" + id + "'";}).join(", "); | |
vfr.query("SELECT Id ,Name ,AnnualRevenue__c ,Company__c ,Description__c " + | |
",Email__c ,Fax__c ,Industry__c ,Status__c ,Salutation__c ,FirstName__c " + | |
",LastName__c ,NoEmployees__c ,Phone__c ,Title__c ,Website__c ,Source__c " + | |
",ServiceAddress__c ,Floor__c ,Unit__c " + | |
"FROM Suspect__c " + | |
"WHERE Id IN (" + myServiceRecordIds +")"). | |
then(function(results) { | |
var suspects = results.records; | |
for (var i=0, max=suspects.length; i < max; i++) { | |
leads[i] = { | |
'LastName': suspects[i].LastName__c, | |
'FirstName': suspects[i].FirstName__c, | |
'Title': suspects[i].Title__c, | |
'Company': suspects[i].Company__c, | |
'Phone': suspects[i].Phone__c, | |
'Fax': suspects[i].Fax__c, | |
'Email': suspects[i].Email__c, | |
'Website': suspects[i].Website__c, | |
'Industry': suspects[i].Industry__c, | |
'AnnualRevenue': suspects[i].AnnualRevenue__c.toString(), | |
'NumberOfEmployees': suspects[i].NoEmployees__c, | |
'ServiceAddress__c': suspects[i].ServiceAddress__c, | |
'Unit__c': suspects[i].Unit__c, | |
'Floor__c': suspects[i].Floor__c, | |
'SuspectId__c': suspects[i].Id | |
}; | |
} | |
return vfr.bulkCreate("Lead", leads); | |
}). | |
then(function(results) { | |
self.reset(); | |
console.log(results); | |
return results; | |
}); | |
} | |
}; | |
self.reset = function() { | |
self.items = []; | |
self.premise = {}; | |
}; | |
createPersistentProperty('items', 'fmServiceItems', Array); | |
createPersistentProperty('premise', 'fmServicePremise', Object); | |
function createPersistentProperty(localName, storageName, Type) { | |
var json = localStorage[storageName]; | |
self[localName] = json ? JSON.parse(json) : new Type; | |
$rootScope.$watch( | |
function() { return self[localName]; }, | |
function(value) { | |
if (value) { | |
localStorage[storageName] = JSON.stringify(value); | |
} | |
}, true); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ot.service('Data', function forceService($rootScope, $q, vfr) { | |
return { | |
getPremise: function() { | |
return vfr.query('SELECT Id, Name, Premise__c, ProximitybookEntry__c, Distance__c ' + | |
',NearestNeighbor__c, ServiceName__c, Proximity__c, ProximityName__c, ProximityColor__c ' + | |
',Premise__r.AddressObject__c ,Premise__r.City__c ' + | |
',Premise__r.CLLICode__c , Premise__r.ClliDistance__c ' + | |
',Premise__r.County__c ,Premise__r.CurrentTenants__c ' + | |
',Premise__r.Customers__c ,Premise__r.DemographicRanking__c ' + | |
',Premise__r.DemographicScore__c ,Premise__r.FormattedAddress__c ' + | |
',Premise__r.HasNeighbors__c ,Premise__r.Id ,Premise__r.Intersection__c ' + | |
',Premise__r.Location__Latitude__s ,Premise__r.Location__Longitude__s ' + | |
',Premise__r.Name ,Premise__r.Neighborhood__c ' + | |
',Premise__r.NonSubscribers__c ,Premise__r.Penetration__c ' + | |
',Premise__r.PremiseClass__c ,Premise__r.PremiseDetail__c ' + | |
',Premise__r.PremiseType__c ,Premise__r.Proximitybook__c ' + | |
',Premise__r.ServicesOnPremise__c ,Premise__r.State__c ' + | |
',Premise__r.StreetAddress__c ,Premise__r.StreetName__c ' + | |
',Premise__r.StreetNumber__c ,Premise__r.Zip__c ' + | |
'FROM Neighbor__c ' + | |
'ORDER BY Premise__r.StreetName__c, Premise__r.StreetNumber__c'). | |
then( | |
function(results) { | |
return results.records; | |
}); | |
} | |
}; | |
}); | |
ot.controller('MapCtrl', | |
function MapCtrl($rootScope, $scope, $q, Data) { | |
var getPrem = Data; | |
getPrem.getPremise().then(function(results) { | |
$scope.premise = results.records; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment