-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separate fetch/save api into plugin or external module #4254
Comments
Hey @taburetkin, thanks for sharing your thoughts!
If you mean the CRUD methods of
I don't really see your point here. What's the difference from
What do you mean by that? I don't think Backbone in its current form is aware of anyone's particular backend.
Honestly, as far as synchronization is concerned, this is very easy to achieve now already because you only need to override |
Yes, i mean crud. Model and Collection should not have any crud related properties and methods like I do not mean that there should not be CRUD at all
Just try this
link to codepen: https://codepen.io/dimatabu/pen/MWOqVGY
oh, thats because of my pure eng, i was telling not about particular backend but about crud operations. i think we can skip this.
actualy any thing is very easy to achieve. and the talk here not about jquery itself. dom manipulation is subject of user choice (there is a good try in marionette about this) perfect minimum application setup should looks like
okay, i can accept next one as semi-perfect
And I don't insist, I just share my thoughts |
Hold on, I think we have a misunderstanding about |
no.
the main thing of destroy is
|
You quote the source code of the Let me suggest a more objective way of identifying the purpose of the
If cleaning up is all you need to do, you can do Is this type of cleaning up really helpful, by the way? I mean, does it actually help in garbage collection, like |
I must agree that backbone view has but lets discuss
in this method I belive that |
By that reasoning, the local I can agree that Now, I can think of three ways we could take this:
|
I think that any decision will be a decision. ps. the main thing of |
CRUD is already abstracted from collection/models, via class BaseModel extends Backbone.Model {
sync() {
// always resolve anytning
}
} |
I guess to some extent this is a question of what the responsibility of Backbone.Model should be:
I would say 'local data' in this context essentially comes down to application state, while 'remote data' refers to some storage layer. Whether this is a remote server or localStorage or whatever. It seems to me what @taburetkin might be aiming for is to remove the responsibility of communicating with a persistence layer from the model entirely? If the model is meant to be a sort of two-way binding with a remote copy of it's data, then it makes sense for communication with the persistence layer (even if indirectly through some layer of abstraction like "feel free to implement your own When it comes to event listeners and such, there is a conceptual difference between 'the value of this attribute of the model changed' and 'a copy of the data contained in this model has just been persisted to/deleted from a storage layer'. With regards to remove/destroy. I would say there is at least potentially a difference between things like cleaning up event listeners, or telling a persistence layer to store or delete some data. Splitting this off into two separate methods might not necessarily be a bad idea, but it really kind of depends on what the model's responsibilities are. Edit: |
Yes, it is.
Yes, this is something that is currently already possible. |
I found some time to re-familiarize myself a little more and indeed, what @ogonkov suggested seems to be all that's needed to remove the responsibility of talking to a persistence layer entirely. I can see there might maybe be one detail to possibly clean up. The method name 'destroy' might imply it's a counterpart to the constructor method. You could argue tearing down the class instance and removing some data from some storage layer should be separate responsibilities. Currently, calling Of course it make sense to tear down the class instance once it's data has been deleted, but maybe it sometimes makes sense t tear it down without deleting the data from your storage layer? Currently the only cleanup that's happening in a standard Backbone model is to call If I'm not mistaken, currently you can only do so by extending var MyFancyModel = Backbone.Model.extend({
destroy: function() {
/* ... some additional cleanup code ... */
Backbone.Model.prototype.destroy.apply(this, arguments);
}
}) But this doesn't allow you to wait until after the data has been deleted. If you need to wait until after the data has been deleted, you'd need to listen for the Perhaps something as simple as spinning off a teardown method would solve this? It would clearly separate the two responsibilities, it'd allow you to execute your additional cleanup behavior after data has been deleted yet before the teardown: function() {
this.stopListening();
},
destroy: function(options) {
options = options ? _.clone(options) : {};
var model = this;
var success = options.success;
var wait = options.wait;
var destroy = function() {
model.teardown();
model.trigger('destroy', model, model.collection, options);
};
options.success = function(resp) {
if (wait) destroy();
if (success) success.call(options.context, model, resp, options);
if (!model.isNew()) model.trigger('sync', model, resp, options);
};
var xhr = false;
if (this.isNew()) {
_.defer(options.success);
} else {
wrapError(this, options);
xhr = this.sync('delete', this, options);
}
if (!wait) destroy();
return xhr;
}, The only thing I'm not really sure about is if this is something you'd actually ever need or if I'm just overthinking... |
That's basically what I've been proposing above as "option 2", except that I called it
That's exactly what I'm unsure about, too. |
Hmm... yeah looking at it from that perspective it's kinda similar to what The option 2 style Meanwhile, I also notice View doesn't do anything to notify other application components that it's has been detached from the DOM and by design it doesn't make any assumptions about how it'll be attached to the DOM. Simplicity and flexibility are clearly the main considerations here. Perhaps splitting off All in all, it's still probably mostly bike-shedding, but at the same time it does feel nice and clean. |
sorry for being absent in this discussion .net: (one of my backend languages is .net) dispose pattern is implemented via I feel that method name should be the same for every class within the framework. btw, destroy - seems to me more natural. |
yes, this will definitely works.
for getting just more backboner model :) |
actually
Line 1213 in 0d455df
|
It would be great if in the second version of the library the model and collection got rid of the built-in backend API.
In my opinion, the backend API should be an external module, although it is possible to provide a base implementation.
Today, you constantly have to look for workarounds if you want to destroy a model that does not imply synchronization, but has the specified Id.
And in general, if we are talking about the model-view concept, in my opinion it would be better if the library will be not aware of the backend
Besides, this separation will simplify the process of getting rid of jQuery if such an idea arises.
The text was updated successfully, but these errors were encountered: