Skip to content

Commit

Permalink
CHG: Remove DataProvider.getInstance() which is buggy
Browse files Browse the repository at this point in the history
  • Loading branch information
farthinker committed Jul 27, 2016
1 parent 651e4c0 commit 1e16db1
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

## V2.1.1 - 2016-07-27

* Remove DataProvider.getInstance() which is buggy

## V2.1.0 - 2016-07-27

* Rewrite the whole component.
Expand Down
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,15 @@ Now, you are ready to go.

If you want to publish new version to npm and bower, please make sure all tests have passed before you publish new version, and you need do these preparations:

* Add new release information in `CHANGELOG.md`. The format of markdown contents will matter, because build scripts will get version and release content from this file by regular expression. You can follow the format of the older release information.
* Check the version number in `bower.json` and `package.json`.

* Put your [personal API tokens](https://github.com/blog/1509-personal-api-tokens) in `/.token.json`, which is required by the build scripts to request [Github API](https://developer.github.com/v3/) for creating new release:
* Add new release information in `CHANGELOG.md`. The format of markdown contents will matter, because build scripts will get version and release content from this file by regular expression. You can follow the format of the older release information.

```json
{
"github": "[your github personal access token]"
}
```
* Put your [personal API tokens](https://github.com/blog/1509-personal-api-tokens) in `/.token`, which is required by the build scripts to request [Github API](https://developer.github.com/v3/) for creating new release.

Now you can run `gulp publish` task, which will do these work for you:
* Commit changes and push.

* Get version number from `CHANGELOG.md` and bump it into `package.json` and `bower.json`.
* Get release information from `CHANGELOG.md` and request Github API to create new release.
Now you can run `gulp publish` task, which will request Github API to create new release.

If everything goes fine, you can see your release at [https://github.com/mycolorway/simple-select/releases](https://github.com/mycolorway/simple-select/releases). At the End you can publish new version to npm with the command:

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple_select",
"version": "2.1.0",
"version": "2.1.1",
"homepage": "https://github.com/mycolorway/simple-select",
"authors": [
"farthinker <[email protected]>"
Expand Down
35 changes: 17 additions & 18 deletions build/publish.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ gulp = require 'gulp'
gutil = require 'gulp-util'
fs = require 'fs'
request = require 'request'
changelogs = require './helpers/changelogs'
# changelogs = require './helpers/changelogs'
handleError = require './helpers/error'
compile = require './compile'
test = require './test'
_ = require 'lodash'

bumpVersion = (done) ->
newVersion = changelogs.lastestVersion
unless newVersion
throw new Error('Publish: Invalid version in CHANGELOG.md')
return

pkg = require '../package'
pkg.version = newVersion
fs.writeFileSync './package.json', JSON.stringify(pkg, null, 2)

bowerConfig = require '../bower.json'
bowerConfig.version = newVersion
fs.writeFileSync './bower.json', JSON.stringify(bowerConfig, null, 2)

done()
bumpVersion.displayName = 'bump-version'
# bumpVersion = (done) ->
# newVersion = changelogs.lastestVersion
# unless newVersion
# throw new Error('Publish: Invalid version in CHANGELOG.md')
# return
#
# pkg = require '../package'
# pkg.version = newVersion
# fs.writeFileSync './package.json', JSON.stringify(pkg, null, 2)
#
# bowerConfig = require '../bower.json'
# bowerConfig.version = newVersion
# fs.writeFileSync './bower.json', JSON.stringify(bowerConfig, null, 2)
#
# done()
# bumpVersion.displayName = 'bump-version'

createRelease = (done) ->
try
Expand Down Expand Up @@ -66,7 +66,6 @@ createRelease.displayName = 'create-release'
publish = gulp.series [
compile,
test,
bumpVersion,
createRelease
]..., (done) ->
done()
Expand Down
31 changes: 14 additions & 17 deletions dist/simple-select.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* simple-select v2.1.0
* simple-select v2.1.1
* http://mycolorway.github.io/simple-select
*
* Copyright Mycolorway Design
Expand Down Expand Up @@ -120,12 +120,10 @@ HtmlSelect = (function(superClass) {
module.exports = HtmlSelect;

},{"./models/group.coffee":4}],2:[function(require,module,exports){
var DataProvider, Input, Item,
var Input, Item,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;

DataProvider = require('./models/data-provider.coffee');

Item = require('./models/item.coffee');

Input = (function(superClass) {
Expand All @@ -137,14 +135,15 @@ Input = (function(superClass) {

Input.prototype.opts = {
el: null,
dataProvider: null,
noWrap: false,
placeholder: '',
selected: false
};

Input.prototype._init = function() {
this.el = $(this.opts.el);
this.dataProvider = DataProvider.getInstance();
this.dataProvider = this.opts.dataProvider;
this._render();
return this._bind();
};
Expand Down Expand Up @@ -306,7 +305,7 @@ Input = (function(superClass) {

module.exports = Input;

},{"./models/data-provider.coffee":3,"./models/item.coffee":5}],3:[function(require,module,exports){
},{"./models/item.coffee":5}],3:[function(require,module,exports){
var DataProvider, Group,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
Expand All @@ -320,10 +319,6 @@ DataProvider = (function(superClass) {
return DataProvider.__super__.constructor.apply(this, arguments);
}

DataProvider.getInstance = function() {
return this.instance;
};

DataProvider.prototype.opts = {
remote: false,
groups: null,
Expand All @@ -333,11 +328,10 @@ DataProvider = (function(superClass) {
DataProvider.prototype._init = function() {
this.remote = this.opts.remote;
if (this.opts.groups) {
this.setGroupsFromJson(this.opts.groups);
return this.setGroupsFromJson(this.opts.groups);
} else if (this.opts.selectEl) {
this.setGroupsFromHtml(this.opts.selectEl);
return this.setGroupsFromHtml(this.opts.selectEl);
}
return DataProvider.instance = this;
};

DataProvider.prototype._fetch = function(value, callback) {
Expand Down Expand Up @@ -793,12 +787,10 @@ MultipleInput = (function(superClass) {
module.exports = MultipleInput;

},{"./input.coffee":2,"./models/item.coffee":5}],7:[function(require,module,exports){
var DataProvider, Group, Item, Popover,
var Group, Item, Popover,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;

DataProvider = require('./models/data-provider.coffee');

Group = require('./models/group.coffee');

Item = require('./models/item.coffee');
Expand All @@ -815,12 +807,14 @@ Popover = (function(superClass) {
Popover.prototype.opts = {
el: null,
groups: [],
dataProvider: null,
onItemRender: null,
localse: {}
};

Popover.prototype._init = function() {
this.el = $(this.opts.el);
this.dataProvider = this.opts.dataProvider;
this.groups = this.opts.groups;
this._render();
return this._bind();
Expand Down Expand Up @@ -980,7 +974,7 @@ Popover = (function(superClass) {

module.exports = Popover;

},{"./models/data-provider.coffee":3,"./models/group.coffee":4,"./models/item.coffee":5}],"simple-select":[function(require,module,exports){
},{"./models/group.coffee":4,"./models/item.coffee":5}],"simple-select":[function(require,module,exports){
var DataProvider, Group, HtmlSelect, Input, Item, MultipleInput, Popover, SimpleSelect,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
Expand Down Expand Up @@ -1048,13 +1042,15 @@ SimpleSelect = (function(superClass) {
if (this.multiple) {
this.input = new MultipleInput({
el: this.wrapper.find('.input'),
dataProvider: this.dataProvider,
placeholder: placeholder,
selected: this.htmlSelect.getValue()
});
groups = this.dataProvider.excludeItems(this.input.selected);
} else {
this.input = new Input({
el: this.wrapper.find('.input'),
dataProvider: this.dataProvider,
placeholder: placeholder,
noWrap: this.opts.noWrap,
selected: this.htmlSelect.getValue()
Expand All @@ -1063,6 +1059,7 @@ SimpleSelect = (function(superClass) {
}
this.popover = new Popover({
el: this.wrapper.find('.popover'),
dataProvider: this.dataProvider,
groups: groups,
onItemRender: this.opts.onItemRender,
locales: this.locales
Expand Down
4 changes: 2 additions & 2 deletions dist/simple-select.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "simple-select",
"githubOwner": "mycolorway",
"version": "2.1.0",
"version": "2.1.1",
"description": "Autocomplete select component",
"homepage": "http://mycolorway.github.io/simple-select",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/input.coffee
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
DataProvider = require './models/data-provider.coffee'
Item = require './models/item.coffee'

class Input extends SimpleModule

opts:
el: null
dataProvider: null
noWrap: false
placeholder: ''
selected: false

_init: ->
@el = $ @opts.el
@dataProvider = DataProvider.getInstance()
@dataProvider = @opts.dataProvider
@_render()
@_bind()

Expand Down
5 changes: 0 additions & 5 deletions src/models/data-provider.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ Group = require './group.coffee'

class DataProvider extends SimpleModule

@getInstance: ->
@instance

opts:
remote: false
groups: null
Expand All @@ -18,8 +15,6 @@ class DataProvider extends SimpleModule
else if @opts.selectEl
@setGroupsFromHtml @opts.selectEl

DataProvider.instance = @

_fetch: (value, callback) ->
return if !@remote || @triggerHandler('beforeFetch') == false

Expand Down
3 changes: 2 additions & 1 deletion src/popover.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DataProvider = require './models/data-provider.coffee'
Group = require './models/group.coffee'
Item = require './models/item.coffee'

Expand All @@ -14,11 +13,13 @@ class Popover extends SimpleModule
opts:
el: null
groups: []
dataProvider: null
onItemRender: null
localse: {}

_init: ->
@el = $ @opts.el
@dataProvider = @opts.dataProvider
@groups = @opts.groups

@_render()
Expand Down
3 changes: 3 additions & 0 deletions src/simple-select.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@ class SimpleSelect extends SimpleModule
if @multiple
@input = new MultipleInput
el: @wrapper.find('.input')
dataProvider: @dataProvider
placeholder: placeholder
selected: @htmlSelect.getValue()
groups = @dataProvider.excludeItems @input.selected
else
@input = new Input
el: @wrapper.find('.input')
dataProvider: @dataProvider
placeholder: placeholder
noWrap: @opts.noWrap
selected: @htmlSelect.getValue()
groups = @dataProvider.groups

@popover = new Popover
el: @wrapper.find('.popover')
dataProvider: @dataProvider
groups: groups
onItemRender: @opts.onItemRender
locales: @locales
Expand Down
2 changes: 1 addition & 1 deletion styles/simple-select.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* simple-select v2.1.0
* simple-select v2.1.1
* http://mycolorway.github.io/simple-select
*
* Copyright Mycolorway Design
Expand Down
62 changes: 62 additions & 0 deletions test/html-select.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
DataProvider = require '../src/models/data-provider'
HtmlSelect = require '../src/html-select'
expect = chai.expect

describe 'Html Select', ->

dataProvider = null
htmlSelect = null

beforeEach ->
dataProvider = new DataProvider
groups: {
'Cat Animals': [
['Cat', '1']
['Tiger', '2']
],
'Dog Animals': [
['Dog', '3']
['Wolf', '4']
]
}

htmlSelect = new HtmlSelect
el: $ '<select>'
groups: dataProvider.getGroups()

afterEach ->
dataProvider = null
htmlSelect = null

it 'accepts element and groups as options', ->
expect(htmlSelect.el.is('select')).to.be.true
expect(htmlSelect.groups.length).to.be.equal 2

it 'will render after setGroups', ->
expect(htmlSelect.el.find('optgroup').length).to.be.equal 2
expect(htmlSelect.el.find('option').length).to.be.equal 4

htmlSelect.setGroups []
expect(htmlSelect.el.html()).to.be.equal '<option></option>'

it 'can get blank option element', ->
$option = htmlSelect.getBlankOption()
expect($option).to.be.false

htmlSelect.setGroups []
$option = htmlSelect.getBlankOption()
expect($option).to.be.not.false
expect($option.is('option')).to.be.true

it 'can get/set value', ->
htmlSelect.setValue '1'
expect(htmlSelect.getValue()).to.be.equal '1'

htmlSelect.setValue ['2']
expect(htmlSelect.getValue()).to.be.equal '2'

htmlSelect.el.attr 'multiple', ''
htmlSelect.setValue '3'
expect(JSON.stringify htmlSelect.getValue()).to.be.equal '["3"]'
htmlSelect.setValue ['3', '4']
expect(JSON.stringify htmlSelect.getValue()).to.be.equal '["3","4"]'
Loading

0 comments on commit 1e16db1

Please sign in to comment.