Knockback.js API and Usage Examples

kb.RefCountable

The base class for all reference-counted classes

Example:

class MyClass extends kb.RefCountable
  constructor: ->
    super
    # do something
    
  __destroy: ->
    # clean up
    super
    
my_instance = new MyClass()  # ref count: 1

my_instance.retain()    # ref count: 2
my_instance.release()   # ref count: 1

my_instance.release()   # ref count: 0 and __destroy() called
var MyClass = kb.RefCountable.extend({
  constructor: function(){
    kb.RefCountable.prototype.constructor.apply(this, arguments);
    // do something
  },
  __destroy: function(){
    // clean up
    kb.RefCountable.prototype.__destroy.apply(this, arguments);
  }
});
var my_instance = new MyClass();  // ref count: 1

my_instance.retain();   // ref count: 2
my_instance.release();  // ref count: 1

my_instance.release();  // ref count: 0 and __destroy() called

kb.RefCountable::extend(properties, [classProperties]) class

Used to derive a class in JavaScript (when CoffeeScript is not used)

Example:

MyClass = kb.RefCountable.extend({
  constructor: (model) ->
    kb.RefCountable.prototype.constructor.apply(this, arguments)
    # to something
})
var MyClass = kb.RefCountable.extend({
  constructor: function(model){
    kb.RefCountable.prototype.constructor.apply(this, arguments);
    // to something
  }
});

constructor

Used to create a new instance

retain()

Increases reference count by 1
my_instance.retain()
my_instance.retain();

refCount()

Returns the current reference count
my_instance.refCount()
my_instance.refCount();

release()

Decreases reference count by 1 and calls __destroy() when ref_count becomes 0
my_instance.release()
my_instance.release();

__destroy() abstract

Called when reference count becomes 0
my_instance = new MyClass()       # ref count: 1
my_instance.release()             # ref count: 0 and __destroy() called
var my_instance = new MyClass();  // ref count: 1
my_instance.release();            // ref count: 0 and __destroy() called