Giter Site home page Giter Site logo

knockout.getset's Introduction

#knockout.getset

Simple library to start using this.property = true; instead of this.property(true); for observables in Knockout.

##Installation

Install from bower:

bower install knockout-getset --save

or npm:

npm install knockout.getset --save

##Compatibility Internet Explorer 9+ and literaly anything else. For more information consult this table.

##Documentation

###Observable To define an observable you need:

var viewmodel = {}, initialValue = 1;
ko.gsObservable(viewmodel, 'observableProperty', initialValue);

Then observableProperty will be available to modification in viewModel object:

viewmodel.observableProperty = 1;
viewmodel.observableProperty; //1

viewmodel.observableProperty = 5;
viewmodel.observableProperty; //5

If you want to register your own subscriptions with gsObservable:

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:

ko.gsObservableArray(viewmodel, 'observableArray');

viewmodel.observableArray; //[]

###Computed To define a computed you need:

ko.gsComputed(viewmodel, 'computed', function(){
    return 'I\'m computed!';
});

viewmodel.computed; //"I'm computed!"

Example on using of computed with observables:

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:

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" }'

knockout.getset's People

Contributors

dnbard avatar

Stargazers

John Gafford avatar Peter Petrov avatar Qazzian avatar Daniel Blendea avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.