Skip to content

Initialise default attributes #322

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

Closed
simonaberry opened this issue Jul 29, 2016 · 6 comments
Closed

Initialise default attributes #322

simonaberry opened this issue Jul 29, 2016 · 6 comments

Comments

@simonaberry
Copy link

simonaberry commented Jul 29, 2016

What is the correct way to set defaults on attributes inside the initialize function

This issue was originally raised in #73 (also #216), and the suggested solution was to use set().

However if i use this.set('fieldname', value); it is not working.... do I have the incorrect format ?

Also - assuming this community has access to the official documentation, it would help to update it (happy to help)

@andrewimm
Copy link
Contributor

Using the initialize() function is highly discouraged. It's supported for legacy reasons, but you should consider it deprecated.

Here's how I would recommend implementing default attributes, with ES6 classes:

class MyObject extends Parse.Object {
  constructor(arguments) {
    super('MyClassName');

    this.set({
      // default values
    });
    if (arguments) {
      this.set(arguments);
    }
  }

  // ... rest of the class
}

(the arguments bit is so that you can still specify initial values to override the defaults)

@simonaberry
Copy link
Author

Thanks for the clarification... do you have a non-ES6 solution ?

@andrewimm
Copy link
Contributor

andrewimm commented Jul 29, 2016

Sorry, I tend to encourage developers to adopt ES6 classes in their workflow. Can you show me how you attempted to implement initialize()? I just threw together a simple example which seems to work fine.

@simonaberry
Copy link
Author

I guess its the fear of the unknown... id did a quick look at ES6 compatibility in browsers and was worried that your ES6 solution may not work in older clients.... but maybe a shim is in order ?

there is the full (angularjs) code for the factory in question:

.factory('FPProject', ['$q', function ($q) {
    var FPProject = Parse.Object.extend('Project', {
        // Instance methods
        initialize: function (attrs, options) {
            //set company if passed
            if ((attrs !== undefined) && (attrs.company !== undefined)) {
                this.set('company', attrs.company);
            }
            this.set('name' , 'New Project');
            this.set('status' , 'a'); //active
        }
    },
    {
         // Class methods           
    });

    // getters & setters
    ['name', 'status', 'company'].forEach(function (item) {
            Object.defineProperty(FPProject.prototype, item, {
                get: function () {
                    return this.get(item);
                },
                set: function (aValue) {
                    return this.set(item, aValue);
                }
            });
        });

    return FPProject;
}]) 

thanks for the help!

@andrewimm
Copy link
Contributor

Okay, I think I figured out what's going on. #81 was intended to fix an issue with initialize, but it broke setting attributes at init time for non-Node.js environments only.

I'll see if I can fix this and push a new version to npm

@simonaberry
Copy link
Author

fixed in 03d14a0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants