Comments (10)
Liking the defaultValue, works well with the select box, automatically selects that option. Though is there anyway I can use that value to pre-fill the input box in autoform?
Apologies - does work something wrong with Meteor hot code reload :(
from meteor-simple-schema.
Can you provide an example use case?
from meteor-simple-schema.
// lets say you create blog posts
new SimpleSchema({
visable: {
type: boolean,
defaultValue: false // not visable if not explicitly told
},
// ...
comments: {
type: [CommentSchema],
defaultValue: [] // should be an empty array on creation
}
});
I can probably think of more examples but you should get the idea.
This would also allow autoform to read defaultValues out of the schema into a form.
The default value could also kick in if you read from the database and the field is not defined (to make sure there is an array instead of undefined) so you can extend your schema later without needing to check everywhere if the field is defined or to create a database query to extend all existing entries.
I also had the idea of making defaultValue a callback like label for multiple reasons.
- The default value must be cloned before inserting it into the data and a callback would let the developer decide if a clone or a reference should be inserted.
- My old translation reason. It would again allow to change the language later and then changing the default accordingly (even though it would not change existing values)
city: {
type: String,
defaultValue: function () {
return (Meteor.isClient && geoLocationVariable)
? geoLocationVariable : __("city.default")
}
}
With autoValue this would not work as the data is (if you documentation is correct) generated on the server which
- does not have access to the Session and therefor the translation fails or falls back to default language (depending on translation toolkit)
- does not have access to the geolocation service of the browser in that example.
from meteor-simple-schema.
But what is the overall context for this example? Because if it's an autoform, then you can use formToDoc
or a before
hook to set default values on the client. If it's something else, then you can theoretically set the default value manually.
I'm not saying this shouldn't be done; it's probably a good idea, but I'd like to have a clear grasp of the legitimate use cases to make sure it's implemented correctly.
It's been suggested also that a more basic autoValue should be part of simple-schema, which might make sense. Then C2 would simply extend the SS autoValue to add on the isInsert
, etc. That idea intertwines with this idea a bit.
from meteor-simple-schema.
Wait, autoValue
and defaultValue
are different. The thing is that defaultValue
is only called (or used) if there is no value and should be added every time clean
is called.
It may be true that there are ways with autoform and collection2 to get this functionality already.
In my specific use case I wanted to set a default values for a city in a user profile as I know where most of my user would come from ( if it wouldn't be an experiment ;) ). I think it's just fitting to put that into the schema with a simple defaultValue: "Berlin"
instead of creating an object with those values set before giving it to the form or database.
from meteor-simple-schema.
Yes, there is value in having a defaultValue
that is not a callback, as a simpler way to do what autoValue
can already do. However, if you need complex logic, then you might as well use the autoValue
that is already available, or use a hook on the client.
So I think I'd be OK with adding defaultValue
that only supports simple non-callback values. Will continue to think about it a bit more, though.
from meteor-simple-schema.
you are probably right... I'm always trying to make things too flexible ;) also my translations probably don't belong into a default value.
however there are problems with a static value. how do you copy it safely (without making references)?
from meteor-simple-schema.
I'll +1 the concept of a defaultValue. As the number of fields we have increases, I don't really want to program hooks or autovalues for everything - just the major cases. Just being able to set a boolean to default to true would be nice (again, simple value, not a callback). Not a must have, but just sounds convenient.
from meteor-simple-schema.
Today's release includes a defaultValue
option, and also moves autoValue
out of Collection2 and into SimpleSchema. See the readme. Let me know if anyone has issues.
from meteor-simple-schema.
I can see no apparent and obvious way to set default values on array fields. I am using a custom template within which I have afArrayEachItem, but when clicking .autoform-add-item, the entry comes up blank, with no preselected value.
tracking a dependency on the form.field.deps, doesn't work without using _.delay, as one cannot guarantee items have been added to the dom reactively, and surely using a default element in the field is the best option.
Any direction would be handy.
Thanks!
from meteor-simple-schema.
Related Issues (20)
- AutoValue Doesn't Run On Server HOT 1
- [solved] custom function validation error HOT 1
- autoValue: calling fields() and siblingFields() yields no results on insert HOT 2
- Overwriting SimpleSchema regEx messages HOT 5
- Error: Invalid definition for inMenu field HOT 1
- Match.Where being instantiated as a class, causes error on Meteor 1.6.1 boot HOT 5
- Make a field conditionally required bug HOT 1
- Explicitly indicate this repo is deprecated HOT 1
- How to define a Custom Validation Error in Autoform >= 6.0 HOT 1
- Removing old field from old users? Ignore schema when using $unset ? HOT 1
- typo in dependencies in version 3.0.0 HOT 1
- 1.6.2 Has removed Deps from Tracker package HOT 4
- Warn / strip global regEx schemas? HOT 1
- the optional: true afFieldInput have the required attribute with it HOT 1
- error in Documentation [max] > {{max}} HOT 1
- "must be an object" error even though typeof() returns Object HOT 3
- $pull not behaving as expected HOT 1
- Move this package to Community Packages to support simpl-schema >=3.x HOT 1
- Update autoValue to accept async function HOT 2
- Typescript error when importing SimpleSchema HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meteor-simple-schema.