Backbone embedded collection event
up vote
0
down vote
favorite
I've created a model (see Delegation) made of basic attributes (name, activity) and one collection (members).
See jsfiddle
The fetch method update the model and trigger a sync event on it - but I would like to be notified when the embedded collection is synced (ie in a real case to render a view when the inner collection is synced).
I tried this
this.listenTo(this.get('members'),'sync',function(){...}
but the corresponding event never fires.
What is the proper way to have it triggered?
javascript backbone.js
add a comment |
up vote
0
down vote
favorite
I've created a model (see Delegation) made of basic attributes (name, activity) and one collection (members).
See jsfiddle
The fetch method update the model and trigger a sync event on it - but I would like to be notified when the embedded collection is synced (ie in a real case to render a view when the inner collection is synced).
I tried this
this.listenTo(this.get('members'),'sync',function(){...}
but the corresponding event never fires.
What is the proper way to have it triggered?
javascript backbone.js
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've created a model (see Delegation) made of basic attributes (name, activity) and one collection (members).
See jsfiddle
The fetch method update the model and trigger a sync event on it - but I would like to be notified when the embedded collection is synced (ie in a real case to render a view when the inner collection is synced).
I tried this
this.listenTo(this.get('members'),'sync',function(){...}
but the corresponding event never fires.
What is the proper way to have it triggered?
javascript backbone.js
I've created a model (see Delegation) made of basic attributes (name, activity) and one collection (members).
See jsfiddle
The fetch method update the model and trigger a sync event on it - but I would like to be notified when the embedded collection is synced (ie in a real case to render a view when the inner collection is synced).
I tried this
this.listenTo(this.get('members'),'sync',function(){...}
but the corresponding event never fires.
What is the proper way to have it triggered?
javascript backbone.js
javascript backbone.js
asked Nov 18 at 20:01
Christian
14318
14318
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
When you execute the
fetch
function of Backbone, it returns a
PROMISE, you can use the function then to launch a function at the
moment thefetch
is finished, you can also use thecatch
for when
it fails.
For example:
this.model = new Backbone.Model()
this.model.url ="example"
this.model.fetch().then((data) => {
console.log("succes: " + data); // Finished the fetch successfully
}).catch((data) =>{
console.log("error: " + data); // The fetch ended in error
})
For your code:
You can use it in this part Backbone.Model.prototype.fetch or if your fetch function returns a Promise, use it when calling the fetch of the model.
New contributor
Hello javimovi, Thank you for taking time to reply to my question. However, I'm not sure I understand your answer and how could the promise be used to trigger the event? Could you give me a full code example or modify the jsfiddle?
– Christian
Nov 19 at 23:44
add a comment |
up vote
0
down vote
I don't see any code that fetches the collection, hence sync
is not triggered on the collection.
You should do delegation1.get('members').fetch()
. For this to work, the collection should have it's own URL as well.
Hello TJ, Thank you, if I understand well, calling fetch on the model containing the collection isn't enough to fetch properly the collection itself? I've just one URL returning both the collection content and the additional parameters (as in the fiddle) var jsonData = { name: "delegation_2", activity: "design", members: [ {firstname: "John", lastname: "DOE"}, {firstname: "Jean", lastname: "Dupont"} ] }; Should I deal with this in the parse method or there is a better technic?
– Christian
Nov 21 at 5:45
" calling fetch on the model containing the collection isn't enough to fetch properly the collection itself" - yes, because backbone doesn't support nested structures out of the box. If your application needs such capabilities a lot, maybe look into backbone plugins such as backbonerelational.org. If you only want minimal capabilities, you can override thefetch/save/destroy
orsync
(for all of them) andparse
to add your custom collection handling - something likesave(){ //your code here; Backbone.model.prototype.save.call(this, arguments);}
– T J
Nov 21 at 12:32
Or just keep it simple and deal with model only: inside model'sparse
make a collection and add as a model attribute, then override the modelstoJSON()
method, calltoJSON()
on the collection, add it to the rest of model attributes and return the combined data structure. In this case your backend for the model should be able to handle model+collection data...
– T J
Nov 21 at 12:38
Thank you for the directions, I'll give a try to the second solution. Backbonerelational is over-weighted regarding my needs.
– Christian
Nov 21 at 16:24
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
When you execute the
fetch
function of Backbone, it returns a
PROMISE, you can use the function then to launch a function at the
moment thefetch
is finished, you can also use thecatch
for when
it fails.
For example:
this.model = new Backbone.Model()
this.model.url ="example"
this.model.fetch().then((data) => {
console.log("succes: " + data); // Finished the fetch successfully
}).catch((data) =>{
console.log("error: " + data); // The fetch ended in error
})
For your code:
You can use it in this part Backbone.Model.prototype.fetch or if your fetch function returns a Promise, use it when calling the fetch of the model.
New contributor
Hello javimovi, Thank you for taking time to reply to my question. However, I'm not sure I understand your answer and how could the promise be used to trigger the event? Could you give me a full code example or modify the jsfiddle?
– Christian
Nov 19 at 23:44
add a comment |
up vote
0
down vote
When you execute the
fetch
function of Backbone, it returns a
PROMISE, you can use the function then to launch a function at the
moment thefetch
is finished, you can also use thecatch
for when
it fails.
For example:
this.model = new Backbone.Model()
this.model.url ="example"
this.model.fetch().then((data) => {
console.log("succes: " + data); // Finished the fetch successfully
}).catch((data) =>{
console.log("error: " + data); // The fetch ended in error
})
For your code:
You can use it in this part Backbone.Model.prototype.fetch or if your fetch function returns a Promise, use it when calling the fetch of the model.
New contributor
Hello javimovi, Thank you for taking time to reply to my question. However, I'm not sure I understand your answer and how could the promise be used to trigger the event? Could you give me a full code example or modify the jsfiddle?
– Christian
Nov 19 at 23:44
add a comment |
up vote
0
down vote
up vote
0
down vote
When you execute the
fetch
function of Backbone, it returns a
PROMISE, you can use the function then to launch a function at the
moment thefetch
is finished, you can also use thecatch
for when
it fails.
For example:
this.model = new Backbone.Model()
this.model.url ="example"
this.model.fetch().then((data) => {
console.log("succes: " + data); // Finished the fetch successfully
}).catch((data) =>{
console.log("error: " + data); // The fetch ended in error
})
For your code:
You can use it in this part Backbone.Model.prototype.fetch or if your fetch function returns a Promise, use it when calling the fetch of the model.
New contributor
When you execute the
fetch
function of Backbone, it returns a
PROMISE, you can use the function then to launch a function at the
moment thefetch
is finished, you can also use thecatch
for when
it fails.
For example:
this.model = new Backbone.Model()
this.model.url ="example"
this.model.fetch().then((data) => {
console.log("succes: " + data); // Finished the fetch successfully
}).catch((data) =>{
console.log("error: " + data); // The fetch ended in error
})
For your code:
You can use it in this part Backbone.Model.prototype.fetch or if your fetch function returns a Promise, use it when calling the fetch of the model.
New contributor
New contributor
answered Nov 19 at 13:56
javimovi
976
976
New contributor
New contributor
Hello javimovi, Thank you for taking time to reply to my question. However, I'm not sure I understand your answer and how could the promise be used to trigger the event? Could you give me a full code example or modify the jsfiddle?
– Christian
Nov 19 at 23:44
add a comment |
Hello javimovi, Thank you for taking time to reply to my question. However, I'm not sure I understand your answer and how could the promise be used to trigger the event? Could you give me a full code example or modify the jsfiddle?
– Christian
Nov 19 at 23:44
Hello javimovi, Thank you for taking time to reply to my question. However, I'm not sure I understand your answer and how could the promise be used to trigger the event? Could you give me a full code example or modify the jsfiddle?
– Christian
Nov 19 at 23:44
Hello javimovi, Thank you for taking time to reply to my question. However, I'm not sure I understand your answer and how could the promise be used to trigger the event? Could you give me a full code example or modify the jsfiddle?
– Christian
Nov 19 at 23:44
add a comment |
up vote
0
down vote
I don't see any code that fetches the collection, hence sync
is not triggered on the collection.
You should do delegation1.get('members').fetch()
. For this to work, the collection should have it's own URL as well.
Hello TJ, Thank you, if I understand well, calling fetch on the model containing the collection isn't enough to fetch properly the collection itself? I've just one URL returning both the collection content and the additional parameters (as in the fiddle) var jsonData = { name: "delegation_2", activity: "design", members: [ {firstname: "John", lastname: "DOE"}, {firstname: "Jean", lastname: "Dupont"} ] }; Should I deal with this in the parse method or there is a better technic?
– Christian
Nov 21 at 5:45
" calling fetch on the model containing the collection isn't enough to fetch properly the collection itself" - yes, because backbone doesn't support nested structures out of the box. If your application needs such capabilities a lot, maybe look into backbone plugins such as backbonerelational.org. If you only want minimal capabilities, you can override thefetch/save/destroy
orsync
(for all of them) andparse
to add your custom collection handling - something likesave(){ //your code here; Backbone.model.prototype.save.call(this, arguments);}
– T J
Nov 21 at 12:32
Or just keep it simple and deal with model only: inside model'sparse
make a collection and add as a model attribute, then override the modelstoJSON()
method, calltoJSON()
on the collection, add it to the rest of model attributes and return the combined data structure. In this case your backend for the model should be able to handle model+collection data...
– T J
Nov 21 at 12:38
Thank you for the directions, I'll give a try to the second solution. Backbonerelational is over-weighted regarding my needs.
– Christian
Nov 21 at 16:24
add a comment |
up vote
0
down vote
I don't see any code that fetches the collection, hence sync
is not triggered on the collection.
You should do delegation1.get('members').fetch()
. For this to work, the collection should have it's own URL as well.
Hello TJ, Thank you, if I understand well, calling fetch on the model containing the collection isn't enough to fetch properly the collection itself? I've just one URL returning both the collection content and the additional parameters (as in the fiddle) var jsonData = { name: "delegation_2", activity: "design", members: [ {firstname: "John", lastname: "DOE"}, {firstname: "Jean", lastname: "Dupont"} ] }; Should I deal with this in the parse method or there is a better technic?
– Christian
Nov 21 at 5:45
" calling fetch on the model containing the collection isn't enough to fetch properly the collection itself" - yes, because backbone doesn't support nested structures out of the box. If your application needs such capabilities a lot, maybe look into backbone plugins such as backbonerelational.org. If you only want minimal capabilities, you can override thefetch/save/destroy
orsync
(for all of them) andparse
to add your custom collection handling - something likesave(){ //your code here; Backbone.model.prototype.save.call(this, arguments);}
– T J
Nov 21 at 12:32
Or just keep it simple and deal with model only: inside model'sparse
make a collection and add as a model attribute, then override the modelstoJSON()
method, calltoJSON()
on the collection, add it to the rest of model attributes and return the combined data structure. In this case your backend for the model should be able to handle model+collection data...
– T J
Nov 21 at 12:38
Thank you for the directions, I'll give a try to the second solution. Backbonerelational is over-weighted regarding my needs.
– Christian
Nov 21 at 16:24
add a comment |
up vote
0
down vote
up vote
0
down vote
I don't see any code that fetches the collection, hence sync
is not triggered on the collection.
You should do delegation1.get('members').fetch()
. For this to work, the collection should have it's own URL as well.
I don't see any code that fetches the collection, hence sync
is not triggered on the collection.
You should do delegation1.get('members').fetch()
. For this to work, the collection should have it's own URL as well.
answered Nov 20 at 10:33
T J
32.3k955108
32.3k955108
Hello TJ, Thank you, if I understand well, calling fetch on the model containing the collection isn't enough to fetch properly the collection itself? I've just one URL returning both the collection content and the additional parameters (as in the fiddle) var jsonData = { name: "delegation_2", activity: "design", members: [ {firstname: "John", lastname: "DOE"}, {firstname: "Jean", lastname: "Dupont"} ] }; Should I deal with this in the parse method or there is a better technic?
– Christian
Nov 21 at 5:45
" calling fetch on the model containing the collection isn't enough to fetch properly the collection itself" - yes, because backbone doesn't support nested structures out of the box. If your application needs such capabilities a lot, maybe look into backbone plugins such as backbonerelational.org. If you only want minimal capabilities, you can override thefetch/save/destroy
orsync
(for all of them) andparse
to add your custom collection handling - something likesave(){ //your code here; Backbone.model.prototype.save.call(this, arguments);}
– T J
Nov 21 at 12:32
Or just keep it simple and deal with model only: inside model'sparse
make a collection and add as a model attribute, then override the modelstoJSON()
method, calltoJSON()
on the collection, add it to the rest of model attributes and return the combined data structure. In this case your backend for the model should be able to handle model+collection data...
– T J
Nov 21 at 12:38
Thank you for the directions, I'll give a try to the second solution. Backbonerelational is over-weighted regarding my needs.
– Christian
Nov 21 at 16:24
add a comment |
Hello TJ, Thank you, if I understand well, calling fetch on the model containing the collection isn't enough to fetch properly the collection itself? I've just one URL returning both the collection content and the additional parameters (as in the fiddle) var jsonData = { name: "delegation_2", activity: "design", members: [ {firstname: "John", lastname: "DOE"}, {firstname: "Jean", lastname: "Dupont"} ] }; Should I deal with this in the parse method or there is a better technic?
– Christian
Nov 21 at 5:45
" calling fetch on the model containing the collection isn't enough to fetch properly the collection itself" - yes, because backbone doesn't support nested structures out of the box. If your application needs such capabilities a lot, maybe look into backbone plugins such as backbonerelational.org. If you only want minimal capabilities, you can override thefetch/save/destroy
orsync
(for all of them) andparse
to add your custom collection handling - something likesave(){ //your code here; Backbone.model.prototype.save.call(this, arguments);}
– T J
Nov 21 at 12:32
Or just keep it simple and deal with model only: inside model'sparse
make a collection and add as a model attribute, then override the modelstoJSON()
method, calltoJSON()
on the collection, add it to the rest of model attributes and return the combined data structure. In this case your backend for the model should be able to handle model+collection data...
– T J
Nov 21 at 12:38
Thank you for the directions, I'll give a try to the second solution. Backbonerelational is over-weighted regarding my needs.
– Christian
Nov 21 at 16:24
Hello TJ, Thank you, if I understand well, calling fetch on the model containing the collection isn't enough to fetch properly the collection itself? I've just one URL returning both the collection content and the additional parameters (as in the fiddle) var jsonData = { name: "delegation_2", activity: "design", members: [ {firstname: "John", lastname: "DOE"}, {firstname: "Jean", lastname: "Dupont"} ] }; Should I deal with this in the parse method or there is a better technic?
– Christian
Nov 21 at 5:45
Hello TJ, Thank you, if I understand well, calling fetch on the model containing the collection isn't enough to fetch properly the collection itself? I've just one URL returning both the collection content and the additional parameters (as in the fiddle) var jsonData = { name: "delegation_2", activity: "design", members: [ {firstname: "John", lastname: "DOE"}, {firstname: "Jean", lastname: "Dupont"} ] }; Should I deal with this in the parse method or there is a better technic?
– Christian
Nov 21 at 5:45
" calling fetch on the model containing the collection isn't enough to fetch properly the collection itself" - yes, because backbone doesn't support nested structures out of the box. If your application needs such capabilities a lot, maybe look into backbone plugins such as backbonerelational.org. If you only want minimal capabilities, you can override the
fetch/save/destroy
or sync
(for all of them) and parse
to add your custom collection handling - something like save(){ //your code here; Backbone.model.prototype.save.call(this, arguments);}
– T J
Nov 21 at 12:32
" calling fetch on the model containing the collection isn't enough to fetch properly the collection itself" - yes, because backbone doesn't support nested structures out of the box. If your application needs such capabilities a lot, maybe look into backbone plugins such as backbonerelational.org. If you only want minimal capabilities, you can override the
fetch/save/destroy
or sync
(for all of them) and parse
to add your custom collection handling - something like save(){ //your code here; Backbone.model.prototype.save.call(this, arguments);}
– T J
Nov 21 at 12:32
Or just keep it simple and deal with model only: inside model's
parse
make a collection and add as a model attribute, then override the models toJSON()
method, call toJSON()
on the collection, add it to the rest of model attributes and return the combined data structure. In this case your backend for the model should be able to handle model+collection data...– T J
Nov 21 at 12:38
Or just keep it simple and deal with model only: inside model's
parse
make a collection and add as a model attribute, then override the models toJSON()
method, call toJSON()
on the collection, add it to the rest of model attributes and return the combined data structure. In this case your backend for the model should be able to handle model+collection data...– T J
Nov 21 at 12:38
Thank you for the directions, I'll give a try to the second solution. Backbonerelational is over-weighted regarding my needs.
– Christian
Nov 21 at 16:24
Thank you for the directions, I'll give a try to the second solution. Backbonerelational is over-weighted regarding my needs.
– Christian
Nov 21 at 16:24
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364902%2fbackbone-embedded-collection-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown