Code Monkey home page Code Monkey logo

Comments (10)

AndyWilkinUK avatar AndyWilkinUK commented on May 18, 2024 1

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.

aldeed avatar aldeed commented on May 18, 2024

Can you provide an example use case?

from meteor-simple-schema.

Nemo64 avatar Nemo64 commented on May 18, 2024
// 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.

  1. 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.
  2. 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

  1. does not have access to the Session and therefor the translation fails or falls back to default language (depending on translation toolkit)
  2. does not have access to the geolocation service of the browser in that example.

from meteor-simple-schema.

aldeed avatar aldeed commented on May 18, 2024

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.

Nemo64 avatar Nemo64 commented on May 18, 2024

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.

aldeed avatar aldeed commented on May 18, 2024

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.

Nemo64 avatar Nemo64 commented on May 18, 2024

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.

aaronjudd avatar aaronjudd commented on May 18, 2024

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.

aldeed avatar aldeed commented on May 18, 2024

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.

vincro avatar vincro commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.