PDA

View Full Version : [FIXED] [1.1.0] Model field truthy defaultValue doesn't allow reading non-truthy value



clarkke8
2 Nov 2011, 11:05 AM
If a field in a model has a default value that is truthy, a non-truthy value can not be read in.

Example:



Ext.regModel("Question", {
fields: [
{name: "id", type: "int"}
,{name: "text", type: "string"}
,{name: "required", type: "boolean", defaultValue: true}
]
,proxy: {
type: 'ajax'
,url: 'questions_get.php'
,reader: {
type: 'json'
,root: 'questions'
}
}
});


questions_get.php:


$questions = array(
array("id"=>1, "text"=>"Question 1 text")
,array("id"=>2, "text"=>"Question 2 text", "required"=>false)
,array("id"=>3, "text"=>"Question 3 text")
,array("id"=>4, "text"=>"Question 4 text")
);

header("Content-type: application/json");
echo json_encode(array("questions"=>$questions));


When the data is read from the server, all records will have required=true, even though I explicitly said I wanted required=false for record id 2.

This line in the Ext.data.Reader class (extractValues function) seems to be the culprit:


value = this.extractorFunctions[i](data) || field.defaultValue;


Since the defaultValue is truthy (in this example, literally true), if the extractor function returns a non-truthy value (in this example, false) then value will be set to the default.

edspencer
3 Nov 2011, 3:53 PM
Thanks, this is fixed in 2.0

clarkke8
4 Nov 2011, 7:18 AM
Thanks.

In the meantime, for users of 1.1.x, here is my workaround:


Ext.data.Reader.override({
extractValues: function(data) {
var fields = this.model.prototype.fields.items,
length = fields.length,
output = {},
field, value, i;

for (i = 0; i < length; i++) {
field = fields[i];
value = this.extractorFunctions[i](data);
if (typeof value == 'undefined') value = field.defaultValue;

output[field.name] = value;
}

return output;
}
});

losingrose
8 Nov 2011, 4:53 PM
Now,you can use the type 'auto' ,it can support 'null' converting to 'defaultValue'.
It work properly in my project.:)