https://github.com/dnbard/knockout.getset
Another way to bind observables for Knockout
https://github.com/dnbard/knockout.getset
Last synced: over 1 year ago
JSON representation
Another way to bind observables for Knockout
- Host: GitHub
- URL: https://github.com/dnbard/knockout.getset
- Owner: dnbard
- Created: 2015-01-28T16:51:10.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-01-31T12:05:40.000Z (over 11 years ago)
- Last Synced: 2025-02-26T06:12:37.346Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 133 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
#knockout.getset
Simple library to start using `this.property = true;` instead of `this.property(true);` for observables in [Knockout](https://github.com/knockout/knockout).
##Installation
Install from `bower`:
```bash
bower install knockout-getset --save
```
or `npm`:
```bash
npm install knockout.getset --save
```
##Compatibility
Internet Explorer 9+ and literaly anything else. For more information [consult this table](http://kangax.github.io/compat-table/es5/#Getter_in_property_initializer).
##Documentation
###Observable
To define an observable you need:
```js
var viewmodel = {}, initialValue = 1;
ko.gsObservable(viewmodel, 'observableProperty', initialValue);
```
Then `observableProperty` will be available to modification in `viewModel` object:
```js
viewmodel.observableProperty = 1;
viewmodel.observableProperty; //1
viewmodel.observableProperty = 5;
viewmodel.observableProperty; //5
```
If you want to register your own subscriptions with `gsObservable`:
```js
ko.gsObservable(viewModel, 'observableProperty', initialValue)
.subscribe(function(newValue){
console.log('I\'ve been changed to %s', newValue);
});
viewmodel.observableProperty = 5;
//I've been changed to 5
```
###Observable Array
To define an observable array you need:
```js
ko.gsObservableArray(viewmodel, 'observableArray');
viewmodel.observableArray; //[]
```
###Computed
To define a computed you need:
```js
ko.gsComputed(viewmodel, 'computed', function(){
return 'I\'m computed!';
});
viewmodel.computed; //"I'm computed!"
```
Example on using of computed with observables:
```js
ko.gsObservable(viewmodel, 'name', 'Peter Griffin');
ko.gsComputed(viewmodel, 'title', function(){
return 'Mr. ' + this.name;
});
viewmodel.title; "Mr. Peter Griffin"
viewmodel.name = 'Brian Griffin';
viewmodel.title; "Mr. Brian Griffin"
```
###Simple objects
Since there is no observables in viewmodel then we can simply skip a 'ko.toJS' or any other unwrap routine. In example:
```js
ko.gsObservable(viewmodel, 'name', 'Peter Griffin');
ko.gsComputed(viewmodel, 'title', function(){
return 'Mr. ' + this.name;
});
JSON.stringify(viewmodel);
//'{ "name":"Peter Griffin", "title":"Mr. Peter Griffin" }'
```