{"__v":6,"_id":"54d9956b4c49f40d00dff98c","category":{"__v":19,"_id":"54d5635632d98b0d00384afe","pages":["54d5635732d98b0d00384b12","54d5635732d98b0d00384b13","54d5635732d98b0d00384b14","54d847c125e90a0d00db551f","54d995416a09052100a6b6d7","54d995534c49f40d00dff98a","54d9956b4c49f40d00dff98c","54dbce210e553a0d00dc1d09","54dcdf7bac05560d00335a15","54dd133c08f6b61700a691f7","54dd136108f6b61700a691fa","54dd27e0db3b362100131294","54dd2838ca1e5219007e9dcb","54dd28c8db3b362100131296","54deb1133a91410d001b1791","54deb134c4a3570d009d4a60","54dec5c0c2b4b70d009c3f05","54ded495e8e35a0d007551a0","54ded57d8e7b000d007c7f31","551447a90edb420d0050016e","551c4369ff892b3700d1c616"],"project":"54d53c7b23010a0d001aca0c","version":"54d5635532d98b0d00384afb","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-02-06T23:44:18.708Z","from_sync":false,"order":4,"slug":"api-plugins","title":"API Plugins"},"parentDoc":null,"project":"54d53c7b23010a0d001aca0c","user":"54cfa8e1a8a4fd0d00b7fd1d","version":{"__v":10,"_id":"54d5635532d98b0d00384afb","forked_from":"54d53c7c23010a0d001aca0f","project":"54d53c7b23010a0d001aca0c","createdAt":"2015-02-07T00:59:01.934Z","releaseDate":"2015-02-07T00:59:01.934Z","categories":["54d5635632d98b0d00384afc","54d5635632d98b0d00384afd","54d5635632d98b0d00384afe","54d5635632d98b0d00384aff","54d5635632d98b0d00384b00","54d5635632d98b0d00384b01","54d5635632d98b0d00384b02","54d652097e05890d006f153e","54dd1315ca1e5219007e9daa","54e21e2b22de1c230094b147","54e68e62a43fe13500db3879","54fa1d3fe7a0ba2f00306309","551c453a23a1ee190034d19a","551df586e52a0b23000c62b6","551f39be6886f8230055f02a","55a6720751457325000e4d97"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.1.0","version":"0.1"},"updates":["5508b33ca223fc0d0076d0a2","57599b5078dabc0e00d47be2"],"next":{"pages":[],"description":""},"createdAt":"2015-02-10T05:21:47.619Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"There are many reasons why plugins need to store persistent data across sessions. Often this can be data specific to a user, the state of the plugin and it's UI or things related to the project. \n\nThe `settings` plugin allows plugins to store settings in a tree like structure, similar to other key value stores like redis or the windows registry. There are three root contexts in which data can be stored. The `user`, `project` and `state`.\n\nUser settings are loaded on each workspace that the user is logged in to. Preference like the selected theme and keyboard shortcuts are stored in the `user` context. Settings that are specific to a project are stored in the `project` context. These settings are stored in the .c9/project.settings relative to the project directory. Settings like the tab distances are stored here. Anyone using that project will load these settings. Finally the application state is stored in the `state` context. Settings like a panel's width or the selection state of the tree are stored here.\n\nAs a plugin developer you choose where to store which setting. First specify the defaults:\n\n```\nsettings.on(\"read\", function(){\n    settings.setDefaults(\"user/my-plugin\", [\n        [\"someKey\", \"value\"],\n        [\"otherKey\", \"value\"]\n    ]);\n}, plugin);\n```\n\nIn this case we're adding some user settings. Set and fetch the values like this:\n\n```\nsettings.set(\"user/my-plugin/:::at:::someKey\", 100);\nsettings.getNumber(\"user/my-plugin/@someKey\");\n```\n\nOften you'll want to listen to changes to a key. For instance when a user changes a setting via the preference pane. To respond to the change of a setting use the following code:\n\n```\nsettings.on(\"user/my-plugin/@someKey\", function(value){\n    console.log(\"Value changed to:\", value);\n}, plugin);\n```\n\nNote that the plugin is passed as the 3rd argument to make sure this event handler is cleaned when the plugin unloads.\n\n## Preferences\n\nWhen you want to enable a user to configure some of these settings, use the preferences API to add some UI elements to the preference pane.\n\nSimply describe the widget, the place of the widget in the navigation tree and the setting it is operating on.\n\n```\nprefs.add({\n    \"Example\" : {\n        position: 450,\n        \"My Plugin\" : {\n            position: 100,\n            \"First Setting\": {\n                type: \"checkbox\",\n                setting: \"user/my-plugin/@first\",\n                position: 100\n            }\n        }\n    }\n}, plugin);\n\n```\n\nSimilar to the menus, use the position index to specify how to sort the vertical position of your widget. \n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Positioning using an index\",\n  \"body\": \"Throughout Cloud9 you'll see APIs that allow you to sort your items using integers. By using an int to set the position it is possible to determine the approximate position of, for instance an item in a menu without having any knowledge about other plugins that are loaded. Usually all plugins are a-like and there are no special ranges. Simply choose a position that makes sense to you and use it. \\n\\nTo see what the position ints are simply load Cloud9 with `?menus=1` in the url.\"\n}\n[/block]\nThe next example show's how to create a dropdown:\n\n```\n\"Second Setting\": {\n    type: \"dropdown\",\n    setting: \"user/my-plugin/@second\",\n    width: \"185\",\n    position: 200,\n    items: [\n        { value: \"you\", caption: \"You\" },\n        { value: \"me\", caption: \"Me\" },\n        { value: \"all\", caption: \"All\" }\n    ]\n}\n```","excerpt":"","slug":"settings-and-preferences","type":"basic","title":"Settings and Preferences"}

Settings and Preferences


There are many reasons why plugins need to store persistent data across sessions. Often this can be data specific to a user, the state of the plugin and it's UI or things related to the project. The `settings` plugin allows plugins to store settings in a tree like structure, similar to other key value stores like redis or the windows registry. There are three root contexts in which data can be stored. The `user`, `project` and `state`. User settings are loaded on each workspace that the user is logged in to. Preference like the selected theme and keyboard shortcuts are stored in the `user` context. Settings that are specific to a project are stored in the `project` context. These settings are stored in the .c9/project.settings relative to the project directory. Settings like the tab distances are stored here. Anyone using that project will load these settings. Finally the application state is stored in the `state` context. Settings like a panel's width or the selection state of the tree are stored here. As a plugin developer you choose where to store which setting. First specify the defaults: ``` settings.on("read", function(){ settings.setDefaults("user/my-plugin", [ ["someKey", "value"], ["otherKey", "value"] ]); }, plugin); ``` In this case we're adding some user settings. Set and fetch the values like this: ``` settings.set("user/my-plugin/@someKey", 100); settings.getNumber("user/my-plugin/@someKey"); ``` Often you'll want to listen to changes to a key. For instance when a user changes a setting via the preference pane. To respond to the change of a setting use the following code: ``` settings.on("user/my-plugin/@someKey", function(value){ console.log("Value changed to:", value); }, plugin); ``` Note that the plugin is passed as the 3rd argument to make sure this event handler is cleaned when the plugin unloads. ## Preferences When you want to enable a user to configure some of these settings, use the preferences API to add some UI elements to the preference pane. Simply describe the widget, the place of the widget in the navigation tree and the setting it is operating on. ``` prefs.add({ "Example" : { position: 450, "My Plugin" : { position: 100, "First Setting": { type: "checkbox", setting: "user/my-plugin/@first", position: 100 } } } }, plugin); ``` Similar to the menus, use the position index to specify how to sort the vertical position of your widget. [block:callout] { "type": "warning", "title": "Positioning using an index", "body": "Throughout Cloud9 you'll see APIs that allow you to sort your items using integers. By using an int to set the position it is possible to determine the approximate position of, for instance an item in a menu without having any knowledge about other plugins that are loaded. Usually all plugins are a-like and there are no special ranges. Simply choose a position that makes sense to you and use it. \n\nTo see what the position ints are simply load Cloud9 with `?menus=1` in the url." } [/block] The next example show's how to create a dropdown: ``` "Second Setting": { type: "dropdown", setting: "user/my-plugin/@second", width: "185", position: 200, items: [ { value: "you", caption: "You" }, { value: "me", caption: "Me" }, { value: "all", caption: "All" } ] } ```