-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Deep setting/getting/observing #11
Comments
It took me a little while to find the viewmodel 😄, and as it stands, it's refreshingly simple. Perhaps there could be an option to plug in viewmodels that implement a prescribed API - kinda like ractive adaptors flipped on their heads? That would let some purpose-built library take over the whacky hijinks involved with |
I think we're thinking on similar lines! Perhaps something like import FirebaseAdaptor from 'svelte-firebase';
const adaptor = new FirebaseAdaptor( someComponent, firebaseRef );
adaptor.set( 'x', y );
adaptor.observe( 'some.deep.keypath', callback ); Big related unsolved question is whether there's a good way to only trigger needed updates in response to a deep set – right now it updates the entire component, and sort of gets away with it because there's not much overhead in the system, but it's not totally ideal. Is most important with lists – in my initial sketches I wondered about exposing component.myList = {
add ( index, items ) {
// insert new iterations, don't update existing ones
},
remove ( index, count ) {
// remove iterations
}
} ...the thinking being that you could build an adaptor-like interface around it similar to |
Was looking into #51, and I think some sort of shared viewmodel-ish thing may become necessary as more complex cases pop up. The nested partials case kinda needs something to handle the nested keypath/data scenario, and if you address that by making partials private pseudo-components, you still have may have issues with SSOT and sibling things pointed at the same data getting out of sync. I don't have a wizard hat though, so I may be a bit off base 😄 |
Any chances for seeing it before 2017? :) Great job with Svelte, but lack of How do you handle such updates in a current version? |
@mefjuu currently you would have to do it in two steps, like so: var so = component.get( 'so' );
so.so.deep.key = 'other';
component.set({ so }); Am hesistant to add it to the core API, but it's something that could be done via a wrapper: function wrap ( component ) {
return {
get ( keypath ) {
const keys = keypath.split( '.' );
let value = component.get( keys.shift() );
while ( keys.length ) value = value[ keys.shift() ];
return value;
},
set ( keypath, value ) {
const keys = keypath.split( '.' );
const lastKey = keys.pop();
const object = this.get( keys.join( '.' ) );
object[ lastKey ] = value;
const data = {};
data[ keys[0] ] = component.get( keys[0] );
component.set( data )
}
};
}
const wrapper = wrap( component );
wrapper.set( 'so.so.deep.key', val ); |
Going to close this issue as I don't think it's necessary to have this in core — could certainly be put into an external wrapper if there's popular demand |
Is there a good pattern for deep observing? |
@aubergene I'd probably create a new method for that. Something like this should work: export default {
methods: {
observeDeep (keypath, callback, opts) {
const parts = keypath.replace(/\[(\d+)\]/g, '.$1').split('.');
let last = undefined;
return this.observe(parts.shift(), newValue => {
for (let i = 0; i < parts.length; i += 1) newValue = newValue[parts[i]];
if (newValue !== last || typeof newValue === 'object' || typeof newValue === 'function') {
callback.call(this, newValue, last);
}
last = newValue;
}, opts);
}
}
}; We could add that to svelte-extras. |
Update: |
Suppose I have a Now, suppose I want to observe any changes in any What would be the proper way to do that in Svelte? Or to rephrase my question : how do I deal with observing changes to the properties of deeply nested components? Adding individual observers to every I suppose a whole lot more elegant approach than a whole bunch of observers would by be to trigger an event in my See also #576 (comment) |
Don't quite know how important this is, and whether it justifies the extra complexity it would entail:
For now I'm inclined not to bother with it, but I'll leave this issue here anyway in case I change my mind.
The text was updated successfully, but these errors were encountered: