Tips



1. AngularJS default value for ng-model

    Use ng-init for setting default value.
    The $scope.value.attr would be created as variable by setting default. If default is not set for value.attr the variable 'attr' would be created in $scope.value unless some value is input by user.

    <tr>
<td class="dataLabel">
Attr&nbsp;:&nbsp;* 
</td>
<td>
<input name="Attr" type="text" value="5" ng-model="value.attr" ng-init="value.attr=0" />
<span class="fieldComment">
</span>
</td>
</tr>




4. Scope variables in controller

Define variable
$scope.value= {};

$scope.value.attr = {};

Define array

$scope.value.comments = [];

5. Loop over scope in controller

$scope.coords = []; 
angular.forEach($scope.properties , function(value){ 
  $scope.coords.push(value.coordinates); 
  console.log(value.coordinates);

  #Wait for REST API Get call to finish before logging for 200 ms
  setTimeout( function(){ 
     console.log(value.coordinates);  
  }, 200); 
});

6. ng components checkbox

7. Nested Model

    Define nested model

    Define nested model serializer 

    There are 5 things to take care.
    1. Define ManytoMany in Model

        product = models.ManyToManyField(ScheduleProduct)

    2. Define many in serializer
    3. Define create function in serializer
    4. Set id in objects.create to prevent 'id not null error'.
    5. Evaluate if read-only is needed.
        owner = ownerSerializer(many=False, read_only=False)

        class ScheduleSerializer(serializers.ModelSerializer):
    product = ScheduleProductSerializer(many=True,)
    oss = ScheduleOperatingSystemSerializer(many=True,)
    class Meta:
        model = Schedule
        fields = ("id","description","cleanRunner","haltCompletion","createSnapshot","deleteScheduleOnCompletion","priority","product","oss","idEPO")
        
    def create(self, validated_data):
        product_data = validated_data.pop('product')
        oss_data = validated_data.pop('oss')
        
        schedule = Schedule.objects.create(**validated_data)
        
        for product in product_data:
            pt = ScheduleProduct.objects.create(idProduct=product.get("idProduct"))
            pt.idProduct = product.get("idProduct")
            pt.name = product.get("name")
            pt.check = product.get("check")
            pt.save()
            schedule.product.add(pt)
            
        for oss in oss_data:
            ot = ScheduleOperatingSystem.objects.create(idOS=oss.get("idOS"))
            ot.idOS = oss.get("idOS")
            ot.name = oss.get("name")
            ot.check = oss.get("check")
            ot.save()
            schedule.oss.add(ot)
            
        schedule.save()
        return schedule


8. 
















Comments