ng.data

0.0.0-rc1 • Public • Published

ng.data: data persistence getter/setter

ng.data aims to make it dead simple to persist data

api

ng.data has a very simple API

ng.data = function(key, value, persist)
{
 
}

get/set

ng.data is just a getter/setter

module('example').controller('ctrl', function(data)
{
    var val = 'Hello!'
 
    //Store the value under key
    data('key', val)
 
    //Get the value: logs Hello!
    console.log(data('key'))
 
    //Delete the value
    data('key', undefined)
})

persistance

If persist is truthy, data is persisted until deleted. On the client this is done with localStorage, on the server this is done with a JSON object.

If persist is undefined, data is persisted in memory only. On the client this is done with sessionStorage, on the server this is done with memory (until server restart).

If persist is falsey (but not undefined), data is stored as flash data. It will be available on the next location path change and then will be erased (this is great for alert messages)

shorthand

Sometimes getters/setters can be annoying if you only want to manipulate data. ng.data has shorthand for these common manipulations

incrementing

verbose

module('example').controller('ctrl', function(data)
{
  var count = data('count')
 
  count += 5
 
  data('count', count)
})

shorthand

module('example').controller('ctrl', function(data)
{
  data('count+', 5)
})

decrementing

verbose

module('example').controller('ctrl', function(data)
{
  var count = data('count')
 
  count -= 5
 
  data('count', count)
})

shorthand

module('example').controller('ctrl', function(data)
{
  data('count-', 5)
})

pushing values

verbose

module('example').controller('ctrl', function(data)
{
  var arr = data('arr')
 
  arr.push('Hello!')
 
  data('arr', arr)
})

shorthand note: this only works if arr is already an array

module('example').controller('ctrl', function(data)
{
  data('arr[]', 'Hello!')
})

adding properties

verbose

module('example').controller('ctrl', function(data)
{
  var obj = data('obj')
 
  obj.prop = 'Hello!'
 
  data('obj', obj)
})

shorthand note: this only works if obj is already an object

module('example').controller('ctrl', function(data)
{
  data('obj.prop', 'Hello!')
})

changelog

0.0.0-rc1

  • Initial commit

todos

  • ???

related projects

  • ng: angular reimagined
  • ng.seed: create a modular ng application using npm packages
  • ng.cql: realtime cassandra database syncing
  • ng.auth: example authentication using ng interceptors
  • ng.crud: example demonstrating a simple crud application using ng.seed
  • ng.style: beautiful html using twitter bootstrap

Readme

Keywords

none

Package Sidebar

Install

npm i ng.data

Weekly Downloads

1

Version

0.0.0-rc1

License

none

Last publish

Collaborators

  • akircher