meelo-store is a javascript library that makes it easy to work with client-side data.
Code on GitHub
It borrows concepts and syntax from jQuery and Django.
A new store was created using the data you see on your right. You can also open your browser's console to see the results.
var Customers = meelo.Store(data);
Customers.length()
To find customers whose name is Nicolas
var nicolas = Customers.filter({
'name': 'Nicolas'
});
nicolas.length()
To get the 23rd customer in the dataset. This returns a Store object not the actual "customer".
var twentythird = Customers.eq(23);
twentythird.name
To get the actual object at a given position.
var twentythird = Customers.get(23);
twentythird.name
To get customers whose age is greater than 27. (You can also use gte, lt, and lte)
var older_than_27 = Customers.filter({
'age__gt': 27
});
older_than_27.length()
Get the first ten and show me their names
older_than_27.slice(0,10).print('name').join(', ')
To get customers whose age is between 24 and 29 (inclusive)
var age_range = Customers.filter({
'age__range': [ 24, 29 ]
})
age_range.length()
To get customers whose age is greater than or equal to 24 OR lower than or equal to 29
var gte24_or_lte29 = Customers.filter({
'age__gte': 24
},{
'age__lte': 29
}
)
gte24_or_lte29.length()
Get customers whose age is greater than or equal to 24 AND whose name starts with O
var gte24_and_nameo = Customers.filter({
'age__gte': 24
, 'name': /^o/i
})
gte24_and_nameo.length()
To get customers who have dog as a favourite animal
var like_dogs = Customers.filter({
'favourite_animals': 'dog'
})
like_dogs.length()
To get customers who have dog AND cat as a favourite animal
var like_dogs_cats = Customers.filter({
'favourite_animals': 'dog'
}).filter({
'favourite_animals': 'cat'
})
like_dogs_cats.length()
To get customers who have dog OR cat as a favourite animal
var like_dogs_or_cats = Customers.filter({
'favourite_animals': 'dog'
},{ 'favourite_animals': 'cat'
})
like_dogs_or_cats.length()
To add new customers
Customers.push([{
'name': 'John'
, 'surname': 'Bonham'
, 'sex': 'male'
, 'email': 'john@bonham.com'
, 'url': 'http://www.bonham.com'
}])
Customers.length()
To change/update customer data
var update_customers = Customers.filter({
'sex': 'other'
}).update({
'sex': '-'
, 'lucky_numbers': function(existing_value){
return [123456789].concat(existing_value);
}
})
update_customers.length()
update_customers = Customers.filter({
'lucky_numbers': 123456789
, 'sex': '-'
})
update_customers.length()
To loop through the customer data
Customers.slice(0, 10).each(function(item, index){
console.log(index+': '+item.name);
})