UNPKG

jive-sdk

Version:

Node.js SDK for Jive Software to assist with the development of add-ons

399 lines (309 loc) 10.7 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Jive SDK Source: jive-sdk-api/lib/tile/definitions.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.spacelab.css"> </head> <body> <div class="container-fluid"> <div class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <a class="brand" href="index.html">Jive SDK</a> <ul class="nav"> <li class="dropdown"> <a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="module-abstractDefinitions.html">abstractDefinitions</a> </li> <li> <a href="module-abstractInstances.html">abstractInstances</a> </li> <li> <a href="module-addOnRoutes.html">addOnRoutes</a> </li> <li> <a href="module-api.html">api</a> </li> <li> <a href="module-community.html">community</a> </li> <li> <a href="module-constants.html">constants</a> </li> <li> <a href="module-devRoutes.html">devRoutes</a> </li> <li> <a href="module-events.html">events</a> </li> <li> <a href="module-extensions.html">extensions</a> </li> <li> <a href="module-extstreamsInstances.html">extstreamsInstances</a> </li> <li> <a href="module-jiveutil.html">jiveutil</a> </li> <li> <a href="module-oauthRoutes.html">oauthRoutes</a> </li> <li> <a href="module-request.html">request</a> </li> <li> <a href="module-security.html">security</a> </li> <li> <a href="module-service.html">service</a> </li> <li> <a href="module-tasks.html">tasks</a> </li> <li> <a href="module-tileInstances.html">tileInstances</a> </li> <li> <a href="module-tileRoutes.html">tileRoutes</a> </li> <li> <a href="module-tilesDefinitions.html">tilesDefinitions</a> </li> <li> <a href="module-webhooks.html">webhooks</a> </li> </ul> </li> <li class="dropdown"> <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="extstreamsDefinitions.html">extstreamsDefinitions</a> </li> <li> <a href="filePersistence.html">filePersistence</a> </li> <li> <a href="memoryPersistence.html">memoryPersistence</a> </li> <li> <a href="memoryScheduler.html">memoryScheduler</a> </li> <li> <a href="oauthHandler.html">oauthHandler</a> </li> </ul> </li> </ul> </div> </div> <div class="row-fluid"> <div class="span12"> <div id="main"> <h1 class="page-title">Source: jive-sdk-api/lib/tile/definitions.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">/* * Copyright 2013 Jive Software * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Library of common methods for manipulating definitions (tiles and extstreams). * @module abstractDefinitions * @abstract */ /////////////////////////////////////////////////////////////////////////////////// // private var q = require('q'); var jive = require('../../api'); var processFound = function(found, expectOne ) { if ( !expectOne ) { return found; } if ( found == null || found.length &lt; 1 ) { return null; } else { // return first one return found[0]; } }; exports.persistence = function() { return jive.context.persistence; }; /** * Save a definition to persistence. If the object does not have an id attribute, a random String is assigned. * If the object is already present in persistence, it will be updated; otherwise it will be inserted. * On success, the returned promise will be resolved with the inserted or updated object; if failure, * the promise will be rejected with an error. * @param {Object} definition * @returns {Promise} Promise */ exports.save = function (definition) { var self = this; var definitionName = definition['name']; // assign a unique id if one doesn't already exist if (!definition.id) { definition.id = jive.util.guid(); } return self.findByTileName( definitionName).then ( function(found ){ if ( found ) { // an existing tile was found jive.logger.info("Updating definition", definitionName ); definition['id'] = found['id']; } else { jive.logger.info("Saving new definition", definitionName ); } return self.persistence().save( self.getCollection(), definition['id'], definition ); }); }; /** * @private */ exports.filterResults = function( results ) { return results; }; /** * @private */ exports.getCollection = function() { throw 'Must be subclassed'; }; /** * Find definitions in persistence using the provided key-value criteria map. For example: * &lt;pre> * { * 'name': 'samplelist' * } * &lt;/pre> * &lt;br> * On success, the returned promise will be resolved with an array of the located objects (which may be * empty if none is found matching the criteria). If failure, * the promise will be rejected with an error. * @param {Object} criteria * @param {Boolean} expectOne If true, the promise will be resolved with at most 1 found item, or null (if none are found). * @returns {Promise} Promise */ exports.find = function ( criteria, expectOne ) { var self = this; return this.persistence().find( self.getCollection(), criteria ).then( function( found ) { found = self.filterResults( found ); return processFound( found, expectOne ); } ); }; /** * Searches persistence for a definition that matches the given ID (the 'id' attribute). * The collection that is searched is defined in subclasses of this class (@see {@link abstractDefinitions:getCollection}). * If one is not found, the promise will resolve a null (undefined) value. * @param {String} definitionID Id of the definition to be retrieved. * @returns {Promise} Promise */ exports.findByID = function (definitionID) { return this.find( { "id": definitionID }, true ); }; /** * Searches persistence for a definition that matches the given name (the 'name' attribute). * The collection that is searched is defined in subclasses of this class (@see {@link abstractDefinitions:getCollection}). * If one is not found, the promise will resolve a null (undefined) value. * @param {String} definitionName Name of the definition to be retrieved. * @returns {Promise} Promise */ exports.findByTileName = function (definitionName) { return this.find( { "name": definitionName }, true ); }; /** * Searches persistence for definitions. * The collection that is searched is defined in subclasses of this class (@see {@link abstractDefinitions:getCollection}). * The promise will resolve with an empty array, or populated array, depending on whether any definitions exist. * @returns {Promise} Promise */ exports.findAll = function () { return this.find( null ); }; /** * Removes a definition from persistence with the specified id (attribute 'id'). * The collection that is searched is defined in subclasses of this class (@see {@link abstractDefinitions:getCollection}). * @param {String} definitionID * @returns {Promise} Promise */ exports.remove = function (definitionID) { return this.persistence().remove( this.getCollection(), definitionID ); }; /** * Adds a listener to the named definition, for the named event. * @param {String} definitionName * @param {String} eventName * @param {function} handler * @param {String} description * @returns {Promise} Promise */ exports.addEventHandler = function(definitionName, eventName, handler, description) { // register event listeners jive.events.registerEventListener( event, handler, { 'eventListener' : definitionName, 'description' : description } ); jive.events.registerEventListener(eventName, definitionName, handler, description); };</pre> </article> </section> </div> <div class="clearfix"></div> <footer> <span class="copyright"> Jive Software, Inc </span> <br /> <span class="jsdoc-message"> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a> on Wed Jan 22 2014 12:29:37 GMT-0800 (PST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>. </span> </footer> </div> <br clear="both"> </div> </div> <script src="scripts/sunlight.js"></script> <script src="scripts/sunlight.javascript.js"></script> <script src="scripts/sunlight-plugin.doclinks.js"></script> <script src="scripts/sunlight-plugin.linenumbers.js"></script> <script src="scripts/sunlight-plugin.menu.js"></script> <script src="scripts/jquery.min.js"></script> <script src="scripts/jquery.scrollTo.js"></script> <script src="scripts/jquery.localScroll.js"></script> <script src="scripts/bootstrap-dropdown.js"></script> <script src="scripts/toc.js"></script> <script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script> <script> $( function () { $( "#toc" ).toc( { selectors : "h1,h2,h3,h4", showAndHide : false, scrollTo : 60 } ); $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); } ); </script> </body> </html>