Skip to content
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

Label values can be defined as both a function or a string #296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,17 @@ var Model = Backbone.Model.extend({
validation: {
someAttribute: {
required: true
},
otherAttribute: {
required: true
}
},

labels: {
someAttribute: 'Custom label'
someAttribute: 'Custom label',
otherAttribute: function() {
return 'Dynamic custom label';
}
}
});
```
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone-validation-amd-min.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions dist/backbone-validation-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,21 +520,28 @@
}).replace(/_/g, ' ');
},

// Looks for a label configured on the model and returns it
// Looks for a label configured on the model and returns it.
// If the label is a function, it will be executed.
//
// var Model = Backbone.Model.extend({
// validation: {
// someAttribute: {
// required: true
// },
// otherAttribute: {
// required: true
// }
// },
//
// labels: {
// someAttribute: 'Custom label'
// otherAttribute: function() {
// return 'Dynamic custom label';
// }
// }
// });
label: function(attrName, model) {
return (model.labels && model.labels[attrName]) || defaultLabelFormatters.sentenceCase(attrName, model);
return (model.labels && _.result(model.labels, attrName)) || defaultLabelFormatters.sentenceCase(attrName, model);
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/backbone-validation-min.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions dist/backbone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,21 +513,28 @@ Backbone.Validation = (function(_){
}).replace(/_/g, ' ');
},

// Looks for a label configured on the model and returns it
// Looks for a label configured on the model and returns it.
// If the label is a function, it will be executed.
//
// var Model = Backbone.Model.extend({
// validation: {
// someAttribute: {
// required: true
// },
// otherAttribute: {
// required: true
// }
// },
//
// labels: {
// someAttribute: 'Custom label'
// otherAttribute: function() {
// return 'Dynamic custom label';
// }
// }
// });
label: function(attrName, model) {
return (model.labels && model.labels[attrName]) || defaultLabelFormatters.sentenceCase(attrName, model);
return (model.labels && _.result(model.labels, attrName)) || defaultLabelFormatters.sentenceCase(attrName, model);
}
};

Expand Down
11 changes: 9 additions & 2 deletions src/backbone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,21 +506,28 @@ Backbone.Validation = (function(_){
}).replace(/_/g, ' ');
},

// Looks for a label configured on the model and returns it
// Looks for a label configured on the model and returns it.
// If the label is a function, it will be executed.
//
// var Model = Backbone.Model.extend({
// validation: {
// someAttribute: {
// required: true
// },
// otherAttribute: {
// required: true
// }
// },
//
// labels: {
// someAttribute: 'Custom label'
// otherAttribute: function() {
// return 'Dynamic custom label';
// }
// }
// });
label: function(attrName, model) {
return (model.labels && model.labels[attrName]) || defaultLabelFormatters.sentenceCase(attrName, model);
return (model.labels && _.result(model.labels, attrName)) || defaultLabelFormatters.sentenceCase(attrName, model);
}
};

Expand Down
14 changes: 12 additions & 2 deletions tests/labelFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ buster.testCase('Label formatters', {
},
some_other_attribute: {
required: true
},
otherAttribute: {
required: true
}
},

labels: {
someAttribute: 'Custom label'
someAttribute: 'Custom label',
otherAttribute: function() {
return 'Dynamic custom label';
}
}
});

Expand Down Expand Up @@ -53,6 +59,10 @@ buster.testCase('Label formatters', {
assert.equals('Custom label is required', this.model.preValidate('someAttribute', ''));
},

"looks up a dynamic label on the model": function(){
assert.equals('Dynamic custom label is required', this.model.preValidate('otherAttribute', ''));
},

"returns sentence cased name when label is not found": function(){
assert.equals('Some attribute is required', this.model.preValidate('some_attribute', ''));
},
Expand Down Expand Up @@ -93,4 +103,4 @@ buster.testCase('Label formatters', {
}
}
}
});
});