{"id":18350478,"url":"https://github.com/dnakov/angular-force","last_synced_at":"2025-04-09T23:53:03.528Z","repository":{"id":75717273,"uuid":"20773026","full_name":"dnakov/angular-force","owner":"dnakov","description":null,"archived":false,"fork":false,"pushed_at":"2014-06-12T19:24:03.000Z","size":140,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T23:52:53.016Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dnakov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-12T15:40:10.000Z","updated_at":"2014-06-12T19:24:03.000Z","dependencies_parsed_at":"2023-02-22T23:30:48.330Z","dependency_job_id":null,"html_url":"https://github.com/dnakov/angular-force","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnakov%2Fangular-force","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnakov%2Fangular-force/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnakov%2Fangular-force/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnakov%2Fangular-force/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dnakov","download_url":"https://codeload.github.com/dnakov/angular-force/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131454,"owners_count":21052819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T21:26:47.178Z","updated_at":"2025-04-09T23:53:03.501Z","avatar_url":"https://github.com/dnakov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"ngForce\n=======\n\nForce.com Angular.js framework.\n\nYou can find the production ready version of the library under js/ngForce/\nngForce depends on the safeApply module that you'll find in safeApply.js under js/ngForce\n\nNote: this is distributed unminified so that you can minify it with the rest of your Angular.js code\n\nUsage\n======\n\nDuring the creation of your application module, inject the ngForce dependcy thusly:\n```javascript\nvar app = angular.module('ngForceDemo', ['ui.bootstrap', 'ui', 'ngForce']);\n```\n\nOnce your app Module has been defined, you can include the service ngForce provides, 'vfr' in any of your controllers by adding it to the dependency injection list like this:\n```javascript\napp.controller('oppBoxCtrl', function($scope, $dialog, vfr)\n```\n\nNote: It probably seems strange that the module is named ngForce, but the service you're injecting is called vfr. ngForce will eventually support not only Visual Force Remoting (VFR) but also javascript ajax calls to the Rest api. The in-progress Rest service will be named, \"sfRestApi\". The module ngForce, however, provides both, hence the odd naming.\n\nThereafter in the controller you can utilize the vfr service much like the $http, or $q services in Angular.\nvfr returns a promise, and therefore your controllers can have a clean(er), less call-back-hell flow to them. Additionally, because promises are binary, you can group vfr callouts and act on that data only once all of the promises have resolved. For example, here's a simple SOQL query returning records via promise:\n\nWhy is this important?\n======================\nThe Deferred / Promise pattern in Angular is a simplified version of the Q library by Kris Kowal (https://github.com/kriskowal/q) It provides a deferred object prototype with, as of Angular.js 1.1.5, just two methods, resolve and reject; and a singular property: promise. \nThe promise object held by the deferred object's promise property has a single method, .then() which is used to complete promises. \nFinally, Angular provides the $q service, which provides the constructor for building deferred objects, as well as an additional two methods, .all() and .when() \n\nSemantically, these are combined with the logic that you Defer some *work* with the *promise* to complete it, and *then* once it's complete, you act on it.\n\nSay more, How do I do that?\n\n```javascript\nvar pOppQuery = vfr.query(\"SELECT Id, Name, Account.Name, LeadSource, Probability, CloseDate, StageName, Amount FROM Opportunity ORDER BY CloseDate DESC\");\npOppQuery.then(function(d) {\n\t$scope.opportunities = d.records;\n});\n```\n\nIn our example above, we're calling the vfr service to make a SOQL query. This is our act of *deferring* some work -- querying Salesforce --. Vfr returns a *promise* to complete that work, which we assign to the variable pOppQuery. We call the .then() method to do some work when our promised work has been completed. \n\nNow, if that was the extent of what you could do with Deferred / Promises it'd be a nice improvement over callback hell. However, the fun doesn't end there. If your .then() method returns a promise, you can create chains promise execution -- enforcing order execution amidst asynchronous work. Here's what that looks like:\n\n```javascript\nvfr.query(\"SELECT Id, Active__c, Site, Type, Industry, Name, AccountNumber, NumberOfEmployees FROM Account\")\n\t\t.then(function(accounts){\n\t\t\t// we can manipulate the results of this first query, even assign scope variables with it\n\t\t\t$scope.accounts = accounts.records;\n\t\t\tvar accountIds = _.pluck(accounts.records, 'Id');\n\t\t\taccountIds = _.map(accountIds, function(id){ return \"'\" + id + \"'\";}).join(\", \");\n\t\t\t// but we must!!! return a promise, like a new ngForce method call\n\t\t\treturn vfr.query(\"SELECT Id, Name FROM Opportunity WHERE AccountId in (\" + accountIds + \")\");\n\t\t}).then(function(Opps){\n\t\t\t$scope.opps = Opps.records;\n\t\t\toppIds = _.pluck(Opps.records, \"Id\"); //shoutout to underscorejs.org!\n\t\t\toppIds = _.map(oppIds, function(id){ return \"'\" + id + \"'\";}).join(\", \");\n\t\t\treturn vfr.query(\"SELECT Id, PricebookEntry.Name, Quantity, UnitPrice FROM OpportunityLineItem WHERE OpportunityId in (\" + oppIds + \")\");\n\t\t}).then(function(products){\n\t\t\t$scope.products = products.records;\n\t\t\treturn products;\n\t\t},\n\t\t// This last link in the chain is our error reporting link.\n\t\t// If / When any of the above promises is rejected, or fails to resolve\n\t\t// this method runs, and in our case logs the error.\n\t\tfunction(error){\n\t\t\tlog(error);\n\t\t});\n```\n\nWhile the above example is contrived, this pattern is extremely useful when you're creating, for instance, an object with several child objects. In the first promise you create the parent object, and in the following promise you create the first child object -- in this second promise you'll have access to the Id of the created object, etc. \n\nFinally, at the very end of the chain, you can append an error handling function. If any of the promises are rejected the following promises will also be rejected passing the error message on to the error function.\n\nAddtionally, the $q service provides the .all() method. If you're familiar with the jQuery Deferred / Promise interface the all() method is functionally identical to the jquery $.when() method. In Angular, you utilize it this way:\n\n```javascript\nvar pQuery1 = vfr.query(\"Select Id from Account\");\nvar pQuery2 = vfr.query(\"Select Id from Contact\");\n\n$q.all(pQuery1, pQuery2).then(function{\n\t// Both of these promises are guaranteed to be completed successfully.\n});\n``` \n\nUsing the helper methods\n========================\n\nngForce provides some helper methods that are intended to make the developers life simpler. Most of these are self explanitory but a couple of them are a bit more esoteric. \n\nPerhaps the most confusing is the bulkCreate method. You can invoke the bulk create method thusly:\n```javascript\nvar pBulkCreateCall = vfr.bulkCreate('objectName__c', dataRows);\npBulkCreateCall.then(function(results){\n\t//do something awesome with results\n});\n\n// dataRows is a numerically key'd object of objects. like this:\n{\n\t\"0\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"1\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"2\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"3\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"4\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"5\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"6\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"7\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"8\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"9\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"10\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"},\n\t\"11\":{\"propA\":3,\"End_date__c\":\"2013-08-21T11:29:27.365Z\"}\n}\n\n// I generated that with: \nvar dataRows = {};\nfor(var i=0; i \u003c 12; i++) {\n\tdataRows[i] = {'propA':3, 'End_date__c': new Date()};\n}\n\n// Each of the child objects should be independently insertable via the vfr.create method -- \n// ie: should be a json representation of the object.\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnakov%2Fangular-force","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdnakov%2Fangular-force","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnakov%2Fangular-force/lists"}