Created
July 20, 2015 13:44
-
-
Save paulwsmith/1b4279681bbc1782dfed to your computer and use it in GitHub Desktop.
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
angular.module('wizehive.resources', ['ngResource', 'wizehive.auth']) | |
.factory('Data', ['$rootScope', '$http', '$q', '$resource', 'Auth', 'PluginEvents', function($rootScope, $http, $q, $resource, Auth, PluginEvents) { | |
var _apiBaseUrl = wizehive.config('constants').API_URL || ''; | |
/* ... */ | |
function beforeFilter(params) { | |
params = _flattenObject(params); | |
if(wizehive.token()) { | |
$http.defaults.headers.common.Authorization = 'Bearer ' + wizehive.token(); | |
} | |
return params; | |
} | |
/* ... */ | |
var _resources = { | |
Activities: ['/activities/:id'], | |
AppTemplates: ['/app_templates/:id'], | |
AppTemplateInstallJobs: ['/app_template_install_jobs/:id'], | |
Calculate: (function() { | |
var _resource = resource('Calculate', '/calculate'); | |
var ret = {}; | |
// query uses POST to send request to API | |
ret.query = function(params, success, error) { | |
return _resource.save({}, params, success, error); | |
}; | |
// No other methods supported for this verb endpoint | |
for (var method in _resource) { | |
ret[method] = ret[method] || unsupportedMethod(method); | |
} | |
return ret; | |
})(), | |
CalculationSettings: ['/calculation_settings/:id'], | |
DataViews: ['/data_views/:id'], | |
Events: ['/events/:id'], | |
Files: ['/files/:id'], | |
Forms: ['/forms/:id'], | |
DefaultFormPermissions: ['/forms/permissions'], | |
FormRecordPermissions: ['/forms/:formId/records/permissions', 'formId'], | |
FormFields: ['/forms/:formId/fields/:id'], | |
FormFolders: ['/forms/:formId/folders/:id'], | |
FormRecords: ['/forms/:formId/records/:id', null, { | |
objectVersionField: 'objectVersion' | |
}], | |
FormUploads: ['/forms/:formId/uploads'], | |
FormFieldTaxonomy: ['/form_field_taxonomy'], | |
RecordImportJobs: ['/record_import_jobs/:id'], | |
RecordExportJobs: ['/record_export_jobs/:id', null], | |
RecordImportFiles: ['/record_import_files/:id', null, { | |
multipart: true | |
}], | |
Roles: ['/workspaces/:workspaceId/roles/:id'], | |
Notes: ['/notes/:id'], | |
NoteReplies: ['/notes/:noteId/replies/:id'], | |
Notifications: ['/notifications/:id'], | |
NotificationEmails: ['/notification_emails/:id'], | |
Tasks: ['/tasks/:id'], | |
TaskLists: ['/task_lists/:id'], | |
TaskPriorities: ['/task_priorities'], | |
TaskStatuses: ['/task_statuses'], | |
Users: ['/users/:id'], | |
TaskPreferences: ['/users/:userId/task_preferences', 'userId'], | |
Workspaces: ['/workspaces/:id'], | |
WorkspaceInvitees: ['/workspaces/:workspaceId/invitees/:id'], | |
WorkspaceMembers: ['/workspaces/:workspaceId/members/:id'], | |
WorkspaceTransferRequests: ['/workspaces/:workspaceId/transfer_requests/:id'], | |
WorkspaceTaskPreferences: ['/workspaces/:workspaceId/members/:memberId/task_preferences', 'memberId'], | |
WorkspaceLogo: ['/workspaces/:workspaceId/logo', 'workspaceId'], | |
WorkspaceCopyJobs: ['/workspace_copy_jobs'], | |
Countries: ['/countries'], | |
States: ['/states'], | |
Subscriptions: ['/subscriptions/:id'], | |
Plugins: ['/plugins/:id', null, { | |
objectVersionField: 'objectVersion' | |
}], | |
PluginScreenshots: ['/plugins/:id/screenshots', 'none'], | |
WorkspacePluginLinks: ['/workspace_plugin_links/:id'], | |
}; | |
var _data = function(name) { | |
if(!(name in _resources)) { | |
throw new Error("Resource '" + name + "' doesn't exist."); | |
} | |
if (name === 'Calculate') { | |
return _resources[name]; | |
} | |
var args = _resources[name]; | |
// add resource name to list of arguments if not already present | |
if (args[0] !== name) { | |
args.unshift(name); | |
} | |
return resource.apply(this, args); | |
}; | |
_data.queue = function(items) { | |
var promise = $q.all(items); | |
return { | |
done: function(callback) { | |
if(typeof callback === 'function') { | |
promise.then(function(args) { | |
callback.apply(promise, args); | |
}); | |
} | |
} | |
}; | |
}; | |
return _data; | |
}]); |
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
// This makes Data factories | |
angular.module('wizehive.resources', ['ngResource', 'wizehive.auth']) | |
.factory('DataFactoryFactory', ['$rootScope', '$http', '$q', '$resource', 'Auth', 'PluginEvents', function($rootScope, $http, $q, $resource, Auth, PluginEvents) { | |
// Actual top level factory function | |
return function(pluginData) { | |
var pluginData = pluginData || null; | |
return function() { | |
var _apiBaseUrl = wizehive.config('constants').API_URL || ''; | |
/* ... */ | |
function beforeFilter(params) { | |
params = _flattenObject(params); | |
if (pluginData) { | |
$http.defaults.headers.common.pluginId = pluginData.id; | |
} | |
if(wizehive.token()) { | |
$http.defaults.headers.common.Authorization = 'Bearer ' + wizehive.token(); | |
} | |
return params; | |
} | |
/* ... */ | |
var _resources = { | |
Activities: ['/activities/:id'], | |
AppTemplates: ['/app_templates/:id'], | |
AppTemplateInstallJobs: ['/app_template_install_jobs/:id'], | |
Calculate: (function() { | |
var _resource = resource('Calculate', '/calculate'); | |
var ret = {}; | |
// query uses POST to send request to API | |
ret.query = function(params, success, error) { | |
return _resource.save({}, params, success, error); | |
}; | |
// No other methods supported for this verb endpoint | |
for (var method in _resource) { | |
ret[method] = ret[method] || unsupportedMethod(method); | |
} | |
return ret; | |
})(), | |
CalculationSettings: ['/calculation_settings/:id'], | |
DataViews: ['/data_views/:id'], | |
Events: ['/events/:id'], | |
Files: ['/files/:id'], | |
Forms: ['/forms/:id'], | |
DefaultFormPermissions: ['/forms/permissions'], | |
FormRecordPermissions: ['/forms/:formId/records/permissions', 'formId'], | |
FormFields: ['/forms/:formId/fields/:id'], | |
FormFolders: ['/forms/:formId/folders/:id'], | |
FormRecords: ['/forms/:formId/records/:id', null, { | |
objectVersionField: 'objectVersion' | |
}], | |
FormUploads: ['/forms/:formId/uploads'], | |
FormFieldTaxonomy: ['/form_field_taxonomy'], | |
RecordImportJobs: ['/record_import_jobs/:id'], | |
RecordExportJobs: ['/record_export_jobs/:id', null], | |
RecordImportFiles: ['/record_import_files/:id', null, { | |
multipart: true | |
}], | |
Roles: ['/workspaces/:workspaceId/roles/:id'], | |
Notes: ['/notes/:id'], | |
NoteReplies: ['/notes/:noteId/replies/:id'], | |
Notifications: ['/notifications/:id'], | |
NotificationEmails: ['/notification_emails/:id'], | |
Tasks: ['/tasks/:id'], | |
TaskLists: ['/task_lists/:id'], | |
TaskPriorities: ['/task_priorities'], | |
TaskStatuses: ['/task_statuses'], | |
Users: ['/users/:id'], | |
TaskPreferences: ['/users/:userId/task_preferences', 'userId'], | |
Workspaces: ['/workspaces/:id'], | |
WorkspaceInvitees: ['/workspaces/:workspaceId/invitees/:id'], | |
WorkspaceMembers: ['/workspaces/:workspaceId/members/:id'], | |
WorkspaceTransferRequests: ['/workspaces/:workspaceId/transfer_requests/:id'], | |
WorkspaceTaskPreferences: ['/workspaces/:workspaceId/members/:memberId/task_preferences', 'memberId'], | |
WorkspaceLogo: ['/workspaces/:workspaceId/logo', 'workspaceId'], | |
WorkspaceCopyJobs: ['/workspace_copy_jobs'], | |
Countries: ['/countries'], | |
States: ['/states'], | |
Subscriptions: ['/subscriptions/:id'], | |
Plugins: ['/plugins/:id', null, { | |
objectVersionField: 'objectVersion' | |
}], | |
PluginScreenshots: ['/plugins/:id/screenshots', 'none'], | |
WorkspacePluginLinks: ['/workspace_plugin_links/:id'], | |
}; | |
var _data = function(name) { | |
if(!(name in _resources)) { | |
throw new Error("Resource '" + name + "' doesn't exist."); | |
} | |
if (name === 'Calculate') { | |
return _resources[name]; | |
} | |
var args = _resources[name]; | |
// add resource name to list of arguments if not already present | |
if (args[0] !== name) { | |
args.unshift(name); | |
} | |
return resource.apply(this, args); | |
}; | |
_data.queue = function(items) { | |
var promise = $q.all(items); | |
return { | |
done: function(callback) { | |
if(typeof callback === 'function') { | |
promise.then(function(args) { | |
callback.apply(promise, args); | |
}); | |
} | |
} | |
}; | |
}; | |
return _data; | |
}; | |
}; | |
}]); | |
angular.module('wizehive.resources', ['ngResource', 'wizehive.auth']) | |
.factory('Data', ['DataFactoryFactory'], function(DataFactoryFactory)) { | |
// Create a default data factory with no plugin data, for regular app use | |
return DataFactoryFactory(null); | |
}]); | |
// In make-plugin.js | |
var newFactory = DataFactoryFactory('myPlugin'); | |
angular.module().factor('myPluginZnData', newFactory); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment