Method used to create a new subclass that inherits from this class. Props to Backbone.js for the code behind this implementation.
- protoProps
- Properties for the prototype of a class
- staticProps
- Static properties for a given object
1 var Dog = Falcon.Object.extend({
2 bark: function()
3 {
4 console.log("The dog barks");
5 }
6 },{
7 says: function() {
8 console.log("Woof!");
9 }
10 });
11
12 var my_dog = new Dog();
13
14 Dog.says(); // Woof!
15 my_dog.bark(); // The dog barks
This is a hash map of non-observable defaults to create whenever an instance of this class is initialized. If the value is a function, the function is called and the return value is assigned. Functions are called in context of this instance and with all the same arguments from the constructor method. Defaults are created before observables which means that any duplicate attributes between the observables (as in the 'observables' attribute on Falcon.Object) and default attributes will be overridden by those in the observables
1 var Dog = Falcon.Object.extend({
2 defaults: {
3 "is_tame": true,
4 "name": function(name) {
5 return ( name || "Fido" );
6 }
7 }
8 });
9
10 var dog = new Dog();
11 console.log( dog.name ); // Fido
12 console.log( dog.is_tame ); // true
13
14 var skip = new Dog("Skip");
15 console.log( skip.name ); // Skip
16 console.log( skip.is_tame ); // true
This is a hash map of the default observables and values that will become properties of this class when a new instance is created. If the value of a specific key is a function a computed observable is created. If the value is an object with the keys 'read' and/or 'write' a computed is created with those object keys as the definition for the computed observable. All computed observables are created with this objects's instance as their context.
1 var Dog = Falcon.Object.extend({
2 observables: {
3 "name": "Fido",
4 "parent_dog_name": "",
5 "child_of_lassie": function(parent_dog_name)
6 {
7 return this.parent_dog_name() === "Lassie";
8 }
9 }
10 });
11
12 var dog = new Dog(); // Dog not related to Lassie
13
14 console.log( ko.isObservable( dog.name ) ); // true
15 console.log( ko.isComputed( dog.child_of_lassie ) ); // true
16
17 console.log( dog.name() ); // Fido
18 console.log( dog.child_of_lassie() ); // false
19
20 dog.name("Spot")
21 dog.parent_dog_name("Lassie")
22
23 console.log( dog.name() ); // Spot
24 console.log( dog.child_of_lassie() ); // true
Adds an event listener with a given function to this Falcon object. Whenever the event is triggered any callback that is bound to this event will also be triggered. This become useful for message passing between objects.
- event
- The event to listen for.
- callback
- The callback function to call when this event is triggered.
- context
- The context to apply to the callback. Defaults to this object.
1 var obj = new Falcon.Object();
2 obj.on("alert", function() { ... });
Removes event listening for a specific event. Optionally, if a callback is given, only that callback will be removed from the list of callbacks for the specified event.
- event
- The event to stop listening for.
- callback
- The specific callback function to remove.
1 var obj = new Falcon.Object();
2
3 obj.on("log", function() {
4 console.log("Hello World");
5 });
6
7 obj.trigger("log"); // Logs 'Hello World'
8
9 obj.off("log");
10
11 obj.trigger("log"); // Nothing Happens
1 var obj = new Falcon.Object();
2
3 var log_callback_one = function() {
4 console.log("Log One");
5 };
6
7 var log_callback_two = function() {
8 console.log("Log Two");
9 };
10
11 obj.on("log", log_callback_one);
12 obj.on("log", log_callback_two);
13
14 obj.trigger("log"); // Logs 'Log One', then 'Log Two'
15
16 obj.off("log", log_callback_one);
17
18 obj.trigger("log"); // Logs 'Log Two'
Triggers an event for this object. This will call all event listeners for a specific event. If any additional arguments are given to this method (other than the event name), those arguments will also be passed on down to the event listeners.
- event
- The event to trigger.
- args...
- Additional arguments to pass on to each of the event listener callback functions.
1 var obj = new Falcon.Object();
2
3 obj.on("log", function()
4 {
5 console.log("Hello World");
6 });
7
8 obj.on("custom_log", function(message)
9 {
10 console.log( message );
11 });
12
13 obj.trigger("log"); // Logs 'Hello World'
14 obj.trigger("custom_log", "Foo Bar"); // Logs 'Foo Bar'
Checks to see if a specific event is defined for an object. Optionally, if a callback is given, this method will check to see if that callback is defined for the specified event.
- event
- The event to check.
- callback
- A specific callback for an event to check.
1 var obj = new Falcon.Object();
2
3 var log_function = function() {
4 log("Hello World")
5 };
6
7 var foo_function = function() {
8 log("Foo Bar")
9 };
10
11 obj.on("log", log_function);
12
13 console.log( obj.has("log") ); // true
14 console.log( obj.has("click") ); // false
15
16 console.log( obj.has("log", log_function) ); // true
17 console.log( obj.has("log", foo_function) ); // false
Adds an event listener onto an object for a certain event while retaining this object's context in the callback. This method is very similar to 'on' but is useful because we're able to store a reference to all of the events that this object relies on. Because we have an internal record of all of the events, we don't need to store references to the original event defintiion if we want to disable those events later. Instead, we can just call the stopListening() method to disable all or a specific set of events from this object.
- object
- The object to listen to.
- event
- The event to listen for.
- callback
- The callback function to call when this event is triggered.
1 var obj = new Falcon.Object();
2 var dog = new Falcon.Model();
3 obj.listenTo(dog, "bark", function() { ... });
Stops listening to all or a sub-set of the current events from this object that were assigned using the 'listenTo' method. If any of the input parameters are given, we'll only stop listening to certain events if they match the input arguments.
- object
- The object to stop listening to.
- event
- The event to stop listening for.
- callback
- Remove events based on a specific event callbakc.
1 var obj = new Falcon.Object();
2 var dog = new Falcon.Model();
3
4 // Stops listening to all events assigned with listenTo
5 obj.stopListening();
6
7 // Stops listening to all events on dog
8 obj.stopListening(dog);
9
10 // Stops listening to all bark events on dog
11 obj.stopListening(dog, "bark");