Skip to content

Commit f12e9d3

Browse files
committed
Button onClick is now evaled in proper scope
It was evaled in the isolated scope of the sf-schema direcive, now we lift it up to the parent outside.
1 parent c5bcfcb commit f12e9d3

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

src/bootstrap-example.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ <h3>Schema</h3>
127127
type: 'actions',
128128
items: [
129129
{ type: 'submit', title: 'Do It!'},
130-
{ type: 'button', title: 'Noooooooooooo'}
130+
{ type: 'button', title: 'Noooooooooooo', onClick: 'sayNo()'}
131131
]
132132
}
133133
];
@@ -167,6 +167,9 @@ <h3>Schema</h3>
167167
};
168168

169169

170+
$scope.sayNo = function() {
171+
alert('Noooooooo')
172+
}
170173

171174
}
172175

src/directives/decorators/bootstrap/actions.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
type="submit"
44
class="btn btn-primary"
55
value="{{item.title}}"
6-
ng-click="buttonClick($event,form)"
76
ng-if="item.type === 'submit'">
87
<button ng-repeat-end class="btn btn-default"
8+
type="button"
99
ng-if="item.type !== 'submit'"
10-
ng-click="buttonClick($event,form)">{{item.title}}</button>
10+
ng-click="buttonClick($event,item)">{{item.title}}</button>
1111
</div>

src/directives/decorators/bootstrap/submit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<input type="submit"
33
class="btn btn-primary"
44
value="{{form.title}}"
5-
ng-click="buttonClick($event,form)"
65
ng-if="form.type === 'submit'">
76
<button class="btn btn-default"
7+
type="button"
88
ng-click="buttonClick($event,form)"
99
ng-if="form.type !== 'submit'">{{form.title}}</button>
1010
</div>

src/directives/schema-form.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ function($compile, schemaForm, schemaFormDecorators){
3232
initialForm: '=sfForm',
3333
model: '=sfModel'
3434
},
35+
controller: ['$scope',function($scope){
36+
this.evalInParentScope = function(expr,locals){
37+
$scope.$parent.$eval(expr,locals);
38+
};
39+
}],
3540
replace: false,
3641
restrict: "A",
3742
transclude: true,

src/services/decorators.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
3838
replace: true,
3939
transclude: false,
4040
scope: true,
41-
link: function(scope,element,attrs) {
41+
require: '?^sfSchema',
42+
link: function(scope,element,attrs,sfSchema) {
4243
//rebind our part of the form to the scope.
4344
var once = scope.$watch(attrs.form,function(form){
4445

@@ -74,11 +75,16 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
7475
return lst;
7576
};
7677

77-
scope.clickButton = function($event,form) {
78+
scope.buttonClick = function($event,form) {
7879
if (angular.isFunction(form.onClick)) {
7980
form.onClick($event,form);
8081
} else if (angular.isString(form.onClick)) {
81-
scope.$eval(form.onClick,{'$event':$event,form:form});
82+
if (sfSchema) {
83+
//evaluating in scope outside of sfSchemas isolated scope
84+
sfSchema.evalInParentScope(form.onClick,{'$event':$event,form:form});
85+
} else {
86+
scope.$eval(form.onClick,{'$event':$event,form:form});
87+
}
8288
}
8389
};
8490
}

src/services/schema-form.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
angular.module('schemaForm').factory('schemaForm',[function(){
77
var service = {};
8-
8+
99
service.merge = function(schema,form,ignore) {
1010
form = form || ["*"];
1111

@@ -24,7 +24,7 @@ angular.module('schemaForm').factory('schemaForm',[function(){
2424
//We look at the supplied form and extend it with schema standards
2525
var lookup = stdForm.lookup;
2626
return form.map(function(obj){
27-
27+
2828
//handle the shortcut with just a name
2929
if (typeof obj === 'string') {
3030
obj = { key: obj };

0 commit comments

Comments
 (0)