UNPKG

2.99 kBJavaScriptView Raw
1var assert = require("assert");
2var BaseCollection = require("../../lib/Collection.js");
3var ExtendedModel = require("../../lib/ExtendedModel.js");
4var ExtendedCollection = require("../../lib/ExtendedCollection.js");
5
6var TestModel = ExtendedModel.extend("TestModel", { id_attribute: "id" });
7var TestCollection = ExtendedCollection.extend("TestCollection", { model: TestModel });
8
9var currentFileMark = ["\t\t\t", "[", __filename, "]", "\n"].join("");
10describe('ExtendedCollection -> bindAll' + currentFileMark, function(){
11
12 it("Using bindAll to listen for model events", function(next){
13 var collection = new TestCollection([
14 { id: 1, field: "a" },
15 { id: 2, field: "a" },
16 { id: 3, field: "b" },
17 { id: 4, field: "b" },
18 { id: 5, field: "b" },
19 ], { group: {field: "field"}} );
20
21 var result, context;
22 collection.bindAll("custom_event", function(data){
23 result = data;
24 context = this;
25 });
26
27 collection.get(1).trigger("custom_event", "text 1");
28 assert.equal( result, "text 1" );
29 assert.equal( context === collection.get(1), true );
30
31 collection.get(2).trigger("custom_event", "text 2");
32 assert.equal( result, "text 2" );
33 assert.equal( context === collection.get(2), true );
34
35 var removed = collection.get(3);
36 collection.remove(3);
37
38 removed.trigger("custom_event", "text 3");
39 assert.equal( result, "text 2" );
40 assert.equal( context === collection.get(2), true );
41
42 var added = collection.add({id: 6, field: "x"});
43 added.trigger("custom_event", "text 6");
44 assert.equal( result, "text 6" );
45 assert.equal( context === collection.get(6), true );
46
47 next();
48 });
49
50 it("Using unbindAll to stop listening", function(next){
51 var collection = new TestCollection([
52 { id: 1, field: "a" },
53 { id: 2, field: "a" },
54 { id: 3, field: "b" },
55 { id: 4, field: "b" },
56 { id: 5, field: "b" },
57 ], { group: {field: "field"}} );
58
59 var result, context;
60 collection.bindAll("custom_event", function(data){
61 result = data;
62 context = this;
63 });
64
65 collection.get(1).trigger("custom_event", "text 1");
66 assert.equal( result, "text 1" );
67 assert.equal( context === collection.get(1), true );
68
69 collection.unbindAll("custom_event");
70
71 collection.get(2).trigger("custom_event", "text 2");
72 assert.equal( result, "text 1" );
73 assert.equal( context === collection.get(1), true );
74
75 var removed = collection.get(3);
76 collection.remove(3);
77
78 removed.trigger("custom_event", "text 3");
79 assert.equal( result, "text 1" );
80 assert.equal( context === collection.get(1), true );
81
82 var added = collection.add({id: 6, field: "x"});
83 added.trigger("custom_event", "text 6");
84 assert.equal( result, "text 1" );
85 assert.equal( context === collection.get(1), true );
86
87 next();
88 });
89
90});