Skip to content

Instantly share code, notes, and snippets.

@darwin
Created July 23, 2008 02:28
Show Gist options
  • Save darwin/1483 to your computer and use it in GitHub Desktop.
Save darwin/1483 to your computer and use it in GitHub Desktop.
// ==========================================================================
// Welcome
// ==========================================================================
Welcome = SC.Object.create({
// This will create the server for your application. Add any namespaces
// your model objects are defined in to the prefix array.
server: SC.Server.create({ prefix: ['Welcome'] }),
// When you are in development mode, this array will be populated with
// any fixtures you create for testing and loaded automatically in your
// main method. When in production, this will be an empty array.
FIXTURES: [],
// Any keys in this array will be instantiated automatically from main.
controllers: []
}) ;
Welcome.Comment = SC.Record.extend(
{
blogType: 'Welcome.Blog'
}) ;
Welcome.Blog = SC.Record.extend(
{
comments: SC.Record.hasMany('Welcome.Comment', 'blog')
}) ;
Welcome.FIXTURES = Welcome.FIXTURES.concat([
{ guid: 1,
type: 'Blog',
label: "B1"
},
{ guid: 2,
type: 'Blog',
label: "B2"
},
{ guid: 3,
type: 'Blog',
label: "B3",
},
{ guid: 1,
type: 'Comment',
label: "C1-1",
blog: 1
},
{ guid: 2,
type: 'Comment',
label: "C2-1",
blog: 1
},
{ guid: 4,
type: 'Comment',
label: "C4-3",
blog: 3
}
]);
function observerForB1(){
var b = SC.Store.findRecords({guid:1}, Welcome.Blog)[0];
var c = b.get('comments');
console.log('event:'+b+', comments.count:'+c.get('count'));
console.log('SC.Store.records:'+SC.Store.records());
}
function observerForB2(){
var b = SC.Store.findRecords({guid:2}, Welcome.Blog)[0];
var c = b.get('comments');
console.log('event:'+b+', comments.count:'+c.get('count'));
console.log('SC.Store.records:'+SC.Store.records());
}
function observerForB3(){
var b = SC.Store.findRecords({guid:3}, Welcome.Blog)[0];
var c = b.get('comments');
console.log('event:'+b+', comments.count:'+c.get('count'));
console.log('SC.Store.records:'+SC.Store.records());
}
function testHasMany(){
var b1 = SC.Store.findRecords({guid:1}, Welcome.Blog)[0];
b1.get('comments').addObserver('count', observerForB1);
console.log('B1.comments.count:', b1.get('comments').get('count'));
var b2 = SC.Store.findRecords({guid:2}, Welcome.Blog)[0];
console.log('B2.comments.count:', b2.get('comments').get('count'));
b2.get('comments').addObserver('count', observerForB2);
var b3 = SC.Store.findRecords({guid:3}, Welcome.Blog)[0];
console.log('B3.comments.count:', b3.get('comments').get('count'));
b3.get('comments').addObserver('count', observerForB3);
console.log('SC.Store.records:'+SC.Store.records());
// add a new comment to blog1
SC.Store.addRecord(Welcome.Comment.create({guid:5, label:'new C5-1', blog:1 }));
}
>>> testHasMany()
B1.comments.count: 2
B2.comments.count: 0
B3.comments.count: 1
SC.Store.records:Welcome.Blog({ guid=1 }),Welcome.Blog({ guid=2 }),Welcome.Blog({ guid=3 }),Welcome.Comment({ guid=1 }),Welcome.Comment({ guid=2 }),Welcome.Comment({ guid=4 })
event:Welcome.Blog({ guid=1 }), comments.count:2
SC.Store.records:Welcome.Blog({ guid=1 }),Welcome.Blog({ guid=2 }),Welcome.Blog({ guid=3 }),Welcome.Comment({ guid=1 }),Welcome.Comment({ guid=2 }),Welcome.Comment({ guid=4 }),Welcome.Comment({ guid=5 })
event:Welcome.Blog({ guid=2 }), comments.count:0
SC.Store.records:Welcome.Blog({ guid=1 }),Welcome.Blog({ guid=2 }),Welcome.Blog({ guid=3 }),Welcome.Comment({ guid=1 }),Welcome.Comment({ guid=2 }),Welcome.Comment({ guid=4 }),Welcome.Comment({ guid=5 })
event:Welcome.Blog({ guid=3 }), comments.count:1
SC.Store.records:Welcome.Blog({ guid=1 }),Welcome.Blog({ guid=2 }),Welcome.Blog({ guid=3 }),Welcome.Comment({ guid=1 }),Welcome.Comment({ guid=2 }),Welcome.Comment({ guid=4 }),Welcome.Comment({ guid=5 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment