我遇到了如何在结果中使用null的嵌套对象处理null异常。我将这些值返回到网格。
示例是迭代。我需要获取迭代对象的name属性。
_onDataLoaded: function(store, data){
var stories = [];
_.each(data, function(story) {
var s = {
FormattedID: story.get('FormattedID'),
Name: story.get('Name'),
Project: story.get('Project').Name,
ScheduleState: story.get('ScheduleState'),
TaskRemainingTotal: story.get('TaskRemainingTotal'),
Blocked: story.get('Blocked'),
Iteration: story.get('Iteration').Name,
Release: story.get('Release').Name,
Predecessor: []
};
var predecessors = story.getCollection('Predecessors', {
fetch:['Rank','FormattedID','Name','Predecessors','Successors','Project','ScheduleState','FormattedID','Blocked','Iteration','Release','TaskRemainingTotal']});
predecessors.load({
callback: function(records, operation, success){
_.each(records, function(predecessors){
s.Predecessor.push({Predecessorformatid: predecessors.get('FormattedID'),
PredecessorsBlocked: predecessors.get('Blocked'),
Predecessorteam: predecessors.get('Project').Name,
PredecessorsState: predecessors.get('ScheduleState'),
PredecessorsTaskActualTotal: predecessors.get('TaskRemainingTotal'),
PredecessorsIteration: predecessors.get('Iteration').Name,
PredecessorsRelease: predecessors.get('Release').Name,
Predecessorname: predecessors.get('Name')
});
}, this);
this._createGrid(stories);
},
scope: this
});
stories.push(s);
}, this);
答案 0 :(得分:0)
如果有可能为null,我通常会这样做:
var iterationName = story.get('Iteration') && story.get('Iteration').Name;
这是有效的,因为如果左右边的&&运算符是假的(其为null)然后运算符将短路而不评估右侧。如果左侧是真实的,那么它将评估并返回右侧。